2

I have a View and inside that view I have a div that will contain a partial view.

My issue is this. The user selects an item from the dropdownlist and I load the partial view with the model. The user change changes some of the textboxes and clicks the button to submit the partial view (which is in a Html.BeginForm).

When I go to examine the model in the controller the model doesn't contain the changes that the user made.

Why doesn't the model reflect the changes the user made?

In the main view:

<div id="personInfo" style="display:none;"></div>

My partial view:

    @model MyProject.MyModel

    @(Html.Kendo().DropDownList().Name("ddlFilters")
                                .AutoBind(true)
                                .OptionLabel("--- Select Filter ---")
                                .DataValueField("ID")
                                .DataTextField("MYFILTER")
                                .DataSource(ds =>
                                    ds.Read(r => r.Action("GetPersonFilters", "Home"))
                                )
                                .Events(x => x.Select("ddlFilters_onSelect"))
                        )

    @using (Html.BeginForm("PersonAction", "Home", FormMethod.Post, new { @class = "form-horizontal", id = "personForm" }))
            {
                 // Strongly typed Kendo fields. Several DropDownListFor and TextBoxFor
                 @Html.Kendo().TextBoxFor(x => x.FirstName).HtmlAttributes(new { @class = "form-control kendoTextBox required " })
                // Button to post the form data to the controller.
            }

My Javascript:

function ddlFilters_onSelect(e) {
    var itm = this.dataItem(e.item);

    clearForm();

    if (itm.ID > 0) {
        // Ajax call to get data....
        $.ajax({
            url: "/Home/GetPerson",
            type: "GET",
            data: { "myID": itm.ID }
        })
        .done(function (result) {
            //var aaa = data;      
            $("#personInfo").html(result);
        })
        .fail(function (xhr, status, err) {
            alert(xhr.responseText);
        });
    }
};

Model:

 public partial class MyModel
    {
        public decimal ID { get; set; }
        public string FirstName{ get; set; }
        public string LastName{ get; set; }
        public string MiddleName{ get; set; }
    }

EDIT: Controller Code:

 // Initial call to main view
    public ActionResult CreateNewPerson()
    {
        return View();
    }

    // Call to load Partial View initially
    public PartialViewResult GetPersonInfo()
    {
        return PartialView("_PersonForm", new MyModel());
    }

    // Call to load partial view with data
    public PartialViewResult GetPerson(int myID)
    {
        myData = GetFromDB(myID);
        return PartialView("_PersonForm", myData);
    }

    // Method to save partial form
    [HttpPost]
    public ActionResult PersonAction(MyModel filter)
    {           

        if (ModelState.IsValid)
        {
            // Go update DB
        }

        return View("CreateNewPerson");
    }
1
  • 1
    Can you post controller code? Commented Apr 25, 2016 at 20:26

1 Answer 1

1

This is not exactly the scenario you described, but this is how my team uses partials:

1) In the ViewModel for your Main View, add a property (e.g. MyModel) for the Model of the partial view.

2) When calling the partial View in the cshtml, make sure you tell MVC where to bind the content of the partial View:

  @Html.Partial("_PersonAction", Model.MyModel, new ViewDataDictionary(Html.ViewData) {
      TemplateInfo = new TemplateInfo { HtmlFieldPrefix = Html.NameFor(m => m.MyModel).ToString() }
  })

Note how we use the TemplateInfo to set the right context for the partial, so the inputs rendered in the partial are prefixed with the correct names to make the modelbinding work. E.g. <input name="MyModel.FirstName"> You can probably fake this in javascript, but don't ask me how.

3) Our controller actions accept the ViewModel of the main page. The <form> is on the main page and surrounds the partial call.

Sign up to request clarification or add additional context in comments.

7 Comments

What do you do when the model is empty the first time? I tried what you said and created a MainModel with a property of MyModel. In the initial view I created a new instance of MainModel and initialized the MyModel prop. (ie new MyModel()) Now when I go to post the data it doesn't recognize any changed values.
I still suspect that the problem is that the name and id of the inputs rendered by the partial are not correctly prefixed (you can confirm this by inspecting the generated HTML). Can you try returning a normal View from GetPerson? Hopefully this solves the problem with the prefixes, and you can keep your original code and model structure.
I'm still new to MVC, but I'm not sure what prefix to expect? When I examine the view I see the form and it's ID and all the fields convert ot input elements with the name and id set to the field in the model.
I created the a method that just returned a view as suggested. I'm still seeing the same behavior so it's got something to do with the way that I'm binding that model.
Thanks for your help. I had a JS method clearing the form prior to the post.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.