默认模型绑定器不填充数据

发布于 2024-11-06 20:51:35 字数 1055 浏览 0 评论 0原文

在这种情况下,默认模型绑定器似乎没有绑定数据。有什么指点吗?

我有一个表单,在提交时调用控制器操作

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

红墙和绿瓦 2024-11-13 20:51:35

您有一个复杂的对象(类中的 PhoneDetails 属性)作为模型。您必须让 MVC 知道哪个字段属于哪个属性,在您的情况下,您必须将文本框名称更改为下面的代码,

<%= Html.TextBox("args.Number") %>
<%= Html.TextBox("args.details.Phone") %>
<%= Html.TextBox("args.details.CountryCode") %>

顺便说一句,您可以将模型展平为仅这三个属性,或者只在操作中接受三个参数方法。然后您可以在视图中保留简单的名称。

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,

<%= Html.TextBox("args.Number") %>
<%= Html.TextBox("args.details.Phone") %>
<%= Html.TextBox("args.details.CountryCode") %>

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.

水染的天色ゝ 2024-11-13 20:51:35

您没有将模型传递给您的部分视图。这样做。

<%Html.RenderPartial("PhoneDetails", Model.PhoneDetails); %>

编辑:

当我看到您的代码表明页面没有继承您想要绑定的正确类时,很难判断如何获取绑定到输入字段的数据。由于使用值渲染文本框应该像这样 <%=Html.TextBox("FieldName", value) %><%=Html.TextBoxFor(model=> ;model.FieldName) %> 我更喜欢后者。

假设您在提交表单后重新绑定字段。由于视图不受 FetchArgs 类的限制,因此您可以将 FetchArgs 类放入 ViewData 中,并使用该 ViewData 将您的字段绑定为模型。

在您的控制器上,我不确定 ActionMethod Fetch 是否返回您示例中的视图或者它被重定向。无论是否重定向,如果 FetchResults 类没有 FetchArgs 成员,您仍然可以将数据放入 ViewData。

如果您在提交后将 FetchArgs 类放入 ViewData 中,则可以在视图中执行此操作。

<%
var fetchArgs = ViewData["FetchArgs"];
%>

<%=Html.TextBox("Number", fetchArgs != null ? fetchArgs.Number : "") %>
<%Html.RenderPartial("PhoneDetails", fetchArgs != null ? fetchArgs.PhoneDetails : null); %>

和您的电话详细信息视图

<%=Html.TextBox("Phone", Model.Phone) %>
<%=Html.TextBox("CountryCode", Model.CountryCode) %>

You are not passing the Model to your Partial View. Do this.

<%Html.RenderPartial("PhoneDetails", Model.PhoneDetails); %>

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 the FetchArgs class in a ViewData 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.

<%
var fetchArgs = ViewData["FetchArgs"];
%>

<%=Html.TextBox("Number", fetchArgs != null ? fetchArgs.Number : "") %>
<%Html.RenderPartial("PhoneDetails", fetchArgs != null ? fetchArgs.PhoneDetails : null); %>

And your PhoneDetails View

<%=Html.TextBox("Phone", Model.Phone) %>
<%=Html.TextBox("CountryCode", Model.CountryCode) %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文