简体   繁体   中英

Default Model Binder not populating data

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

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) %>

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM