如果我有视图和部分视图,有什么方法可以将数据从部分视图传递给父视图?
所以,如果我有View.cshtml
:
<div id="@someDataFromPartialSomehow">
@Html.Partial("_PartialView")
</div>
Run Code Online (Sandbox Code Playgroud)
而且_PartialView.cshtml
:
@{ someDataFromPartialSomehow = "some-data" }
<div>
Some content
</div>
Run Code Online (Sandbox Code Playgroud)
我将如何实现这样的事情?
我尝试使用ViewBag.SomeDataFromPartialSomehow
,但这只是导致null
父.
一次尝试
要尝试解决在调用之前生成的数据问题,我试过这个:
View.cshtml
:
@{ var result = Html.Partial("_PartialView"); }
<div id="@ViewData["Stuff"]">
@result
<div>
Run Code Online (Sandbox Code Playgroud)
_PartialView.cshtml
:
@{ ViewData["Stuff"] = "foo"; }
<div>
Content
</div>
Run Code Online (Sandbox Code Playgroud)
但@ViewDate["Stuff"]
不幸的是,召唤仍然没有任何结果.
我试图在VS2008中创建一个简单的用户控件(不是WPF),它实际上是SplitContainer
一个按钮Panel1
,当按下时,切换Panel2Collapsed
属性并将控件的大小调整为大小Panel1
.
以下是控件的基本内容:
private int _openHeight;
private int _closedHeight;
public MyUserControl(bool open)
{
InitializeComponent();
_openHeight = this.Height;
_closedHeight = splitContainer1.SplitterDistance;
Open = open;
}
private bool _open;
private bool Open
{
get { return _open; }
set
{
_open = value;
splitContainer1.Panel2Collapsed = !_open;
this.Height = _open ? _openHeight : _closedHeight;
}
}
private void button1_Click(object sender, EventArgs e)
{
Open = !Open;
}
Run Code Online (Sandbox Code Playgroud)
问题是,this.Height
Runtime是控件在用户控件设计器中的值,而不是主窗体设计器中设计时设置的值.
任何帮助将不胜感激.
UPDATE
继Lucas的解决方案之后,这种方式意味着_openHeight只设置一次,从而产生了预期的效果:
private int? _openHeight; …
Run Code Online (Sandbox Code Playgroud) 是否可以反序列化二进制文件的一部分?
基本上我有一个类似于下面的对象,我将其序列化为二进制文件.
public class MyObject
{
public string Name { get; set; }
public int Value { get; set; }
public IList<MyOtherObject> { get; set; } // lots of data in here (order of kB-MB)
}
Run Code Online (Sandbox Code Playgroud)
我想要的是能够仅反序列化Name
并Value
通过填充ListView
文件选择目的,然后在需要时反序列化文件的其余部分(即用户从中选择该文件ListView
).
与往常一样,任何帮助都非常感激,如果建议任何第三方库,他们需要能够在商业环境中自由使用.
在编写程序时,我遇到了在我的一个函数中找到数字的立方根.
当我使用下面的代码时,我得到了一个不正确的多维数据集根值(1
正在打印n = 64
).
public static void cubicPairs(double n)
{
double root = (System.Math.Pow(n, (1/3)));
Console.WriteLine(root);
}
Run Code Online (Sandbox Code Playgroud)
现在我把代码稍微改成了这个,
public static void cubicPairs(double n)
{
double root = (System.Math.Pow(n, (1.0/3.0))); //Changed how second parameter is passed
Console.WriteLine(root);
}
Run Code Online (Sandbox Code Playgroud)
我得到root = 3.9999999999999996
(调试时),但方法是打印4
(这是正确的).
为什么这两个值之间存在差异,如果这与方法的第二个参数有关System.Math.Pow()
(即,1.0/3.0
这是一个递归值),我应该使用什么来查找多维数据集根以便我得到4
(在调试时)而不是3.9999999999999996
?
我有以下工厂:
FactoryGirl.define do
factory :foo do
sequence(:name) { |n| "Foo #{n}" }
trait :y do
sequence(:name) { |n| "Fooy #{n}" }
end
end
end
Run Code Online (Sandbox Code Playgroud)
如果我跑
create :foo
create :foo
create :foo, :y
Run Code Online (Sandbox Code Playgroud)
我得到Foo 1, Foo 2, Fooy 1
.但我想要Foo1, Foo2, Fooy 3
.我怎样才能做到这一点?
是否可以仅更改“ BuildConfig
仪器测试”中的字段?
这背后的当前动机是,我想将应用程序的URL指向仅用于Instrumentation测试的另一台服务器(即本地模拟服务器)。这些网址是通过buildConfigField
中的build.gradle
进行配置的,用于多种应用。
我试图从CollectionChanged
实现的集合事件中获取一些自定义对象INotifyCollectionChanged
.
MyControl_MyCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if(e.Action == NotifyCollectionChangedAction.Add)
{
lock(e.NewItems.SyncRoot)
{
var myItems = e.NewItems.OfType<MyType>();
if(myItems.Any())
{
//do stuff
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我面临的问题是myItems
总是说"枚举没有结果".
扩展调试e.NewItems.SyncRoot
显示以下内容:
e.NewItems.SyncRoot | {object[1]}
|-[0] | {System.Linq.Enumerable.WhereSelectListIterator<MyType, IMyInterface>}
| |-base ...
| |-Non-public members
| |-Results View | Expanding the Results View...
| |-[0] | MyType
Run Code Online (Sandbox Code Playgroud)
很明显,数据就在那里.检索此数据的方法是什么?
我对LINQ to SQL有一个特殊的问题:
这样做很好:
from s in Something
join a in AnotherThing
on s.NullableDateTime.Value
equals a.DateTime
select s
Run Code Online (Sandbox Code Playgroud)
但是,使用匿名类型如下:
from s in Something
join a in AnotherThing
on new { s.NullableDateTime.Value }
equals new { a.DateTime }
select s
Run Code Online (Sandbox Code Playgroud)
结果是
join子句中某个表达式的类型不正确.调用"加入"时类型推断失败.
我需要使用匿名类型,因为我的目标是添加另一列来加入.
有关为什么会发生这种情况以及如何解决的任何想法?
我目前正试图了解所有不同的设计模式,我已经设置了IQueryable
基于不同列排序的任务,这是当前实现的方式:
if (choice == 1)
{
return from Animals in ctx.Animals orderby Animals.AnimalID descending select Animals;
}
else if (choice == 2)
{
return from Animals in ctx.Animals orderby Animals.Name descending select Animals;
}
else if (choice == 3)
{
return from Animals in ctx.Animals orderby Animals.Age descending select Animals;
}
Run Code Online (Sandbox Code Playgroud)
然而,这似乎是一个糟糕的代码味道,它没有能力轻松添加不同的字段或排序选项,我的导师告诉我,实施策略模式并使用a Dictionary
选择策略实现我们会更好但是,我不确定战略模式将如何应用于这种情况,任何有用的提示将非常感激,如果需要更多信息,请问.
我刚刚将一个项目克隆到一台新机器上,并且我在NullReferenceException
使用OWIN的MVC网站上进行了难以调试:
[NullReferenceException: Object reference not set to an instance of an object.]
Microsoft.Owin.Security.Cookies.<AuthenticateCoreAsync>d__0.MoveNext() +664
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +93
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +52
System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() +24
Microsoft.Owin.Security.Infrastructure.<BaseInitializeAsync>d__2.MoveNext() +860
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +93
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +52
System.Runtime.CompilerServices.TaskAwaiter.GetResult() +21
Microsoft.Owin.Security.Infrastructure.<Invoke>d__0.MoveNext() +427
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() +22
Microsoft.Owin.Host.SystemWeb.Infrastructure.ErrorState.Rethrow() +33
Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.StageAsyncResult.End(IAsyncResult ar) +150
Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.IntegratedPipelineContext.EndFinalWork(IAsyncResult ar) +42
System.Web.AsyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +415
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155
Run Code Online (Sandbox Code Playgroud)
它发生在之后
[assembly: OwinStartupAttribute(typeof(Website.Startup))]
namespace Website
{
public partial class Startup
{
public void Configuration(IAppBuilder app)
{
ConfigureAuth(app);
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是在我抛出异常之后,这就像我可以辨别为F10ing一样 - 它永远不会到达第一个控制器的构造函数.
有没有人经历过这个和/或能指出我可能导致错误的方向?
c# ×8
linq ×2
.net-3.5 ×1
android ×1
asp.net-mvc ×1
factory-bot ×1
linq-to-sql ×1
owin ×1
pow ×1
razor ×1
ruby ×1