默认模型绑定器不填充数据
在这种情况下,默认模型绑定器似乎没有绑定数据。有什么指点吗?
我有一个表单,在提交时调用控制器操作
public ActionResult Fetch(FetchArgs args)
FetchArgs 定义为:
public class FetchArgs
{
public int Number { get; set; }
public PhoneDetails details {get;set;}
}
public class PhoneDetails
{
public int Phone { get; set; }
public int CountryCode {get;set;}
}
表单看起来像 -
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<FetchResults>" %>
//Other code
<%= Html.TextBox("Number")%> //Value gets set by model binder
<%Html.RenderPartial("PhoneDetails"); %>
PhoneDetails 视图看起来像 -
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<PhoneDetails>" %>
//Other code
<%= Html.TextBox("Phone")%> //Value does not get set by model binder in controller action
<%= Html.TextBox("CountryCode")%> //Value does not get set by model binder
The default model binder does not seem to bind data in this scenario. Any pointers?
I have a form which on submit invokes the controller action
public ActionResult Fetch(FetchArgs args)
FetchArgs is defined as:
public class FetchArgs
{
public int Number { get; set; }
public PhoneDetails details {get;set;}
}
public class PhoneDetails
{
public int Phone { get; set; }
public int CountryCode {get;set;}
}
The form looks like -
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<FetchResults>" %>
//Other code
<%= Html.TextBox("Number")%> //Value gets set by model binder
<%Html.RenderPartial("PhoneDetails"); %>
PhoneDetails view looks like -
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<PhoneDetails>" %>
//Other code
<%= Html.TextBox("Phone")%> //Value does not get set by model binder in controller action
<%= Html.TextBox("CountryCode")%> //Value does not get set by model binder
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您有一个复杂的对象(类中的 PhoneDetails 属性)作为模型。您必须让 MVC 知道哪个字段属于哪个属性,在您的情况下,您必须将文本框名称更改为下面的代码,
顺便说一句,您可以将模型展平为仅这三个属性,或者只在操作中接受三个参数方法。然后您可以在视图中保留简单的名称。
You have a complex object(PhoneDetails property within the class) as model. You have to let MVC know which field goes to which property, in your case you have to change the textbox names as the code below,
BTW, you could just flatten your model to just these three properties, or just accept three parameters in you action method. Then you can keep the simple names in the view.
您没有将模型传递给您的部分视图。这样做。
编辑:
当我看到您的代码表明页面没有继承您想要绑定的正确类时,很难判断如何获取绑定到输入字段的数据。由于使用值渲染文本框应该像这样
<%=Html.TextBox("FieldName", value) %>
或<%=Html.TextBoxFor(model=> ;model.FieldName) %>
我更喜欢后者。假设您在提交表单后重新绑定字段。由于视图不受
FetchArgs
类的限制,因此您可以将FetchArgs
类放入ViewData
中,并使用该 ViewData 将您的字段绑定为模型。在您的控制器上,我不确定 ActionMethod Fetch 是否返回您示例中的视图或者它被重定向。无论是否重定向,如果 FetchResults 类没有 FetchArgs 成员,您仍然可以将数据放入 ViewData。
如果您在提交后将 FetchArgs 类放入 ViewData 中,则可以在视图中执行此操作。
和您的电话详细信息视图
You are not passing the Model to your Partial View. Do this.
EDIT:
When I see your code that the page is not inheriting the right class you want to bind, it is hard to tell how you get the data bounded to the input fields. Since rendering the textbox with a value should be like this
<%=Html.TextBox("FieldName", value) %>
or<%=Html.TextBoxFor(model=>model.FieldName) %>
which I prefer the later.Assuming you are rebinding the Fields after you submit the form. Since the View is not bounded to
FetchArgs
class, you could put theFetchArgs
class in aViewData
and use that ViewData to bind your Fields as model.At your controller, I am not sure if the ActionMethod Fetch is returning the View at your example or it is redirected. Redirected or not, you can still put your data to a ViewData if
FetchResults
class don't have a member of FetchArgs.If you have put your FetchArgs class in a ViewData after you submit you can then in your View do this.
And your PhoneDetails View