Mallikarjuna Golla
Mallikarjuna Golla

Reputation: 49

How to Post Partial View Data?

Any input much appreciated :)

I want to know one thing whether I can post multiple partial views data in MVC?(means i want to update partial views data to DATABASE)

Here is the Example:

Model:-

public class PassengerViewModel
{
    public List<PassengerModel> Passengers { get; set; }
    public ContactModel Contact { get; set; }
}

Controller:-

[RequiredAuthentication]
    public ActionResult Passenger()
    {
        var passengrViewMdl = new PassengerViewModel()
        {
            Contact = new ContactModel(),
            Passengers = psngrService.LoadPassengers(Convert.ToInt32(Session["LPORefNO"]))
        };
        return View(passengrViewMdl);
    }
    [HttpPost]
    public ActionResult Passenger(PassengerViewModel passengerViewModel)
    {
        Here i want to update Passengers & Contact information
    }

View:-

@model QR.LPO.Core.Models.PassengerViewModel
@{
ViewBag.Title = "Add Passengers";
}
@using (Html.BeginForm())
{
@Html.Partial("_Passenger", Model.Passengers);
@Html.Partial("_PassengerContact", Model.Contact);
<input type="submit" value="Submit" />
}

Thanks.

Upvotes: 3

Views: 4144

Answers (2)

bhamlin
bhamlin

Reputation: 5187

This will almost work as you have it - there's nothing inherent to partials that would prevent this, in the end the html that's output is all that's important.

The problem with your code is that presumably the model of your _Passenger view is of type Passengers and the model of your _PassangerContact view is of type Contact. What this means is that if you standard HtmlHelper extensions (like Html.Textbox(...) or Html.TextboxFor(...) the fields they generate will not have full names like Contact.Name, but instead just names relative to their model, like Name. This will cause modelbinding to fail in your post action.

You can solve this in a number of ways.

  1. Simply use the same model type (PassengerViewModel) in your sub-views, and write code like @Html.TextboxFor(m => m.Contact.Name).

  2. Instead of using Html.Partial, use Html.EditorFor(...). This passes the proper prefix information into the child view so the field names are generated properly.

  3. Explicitly set the prefix yourself

Like this:

@{
    var childViewData = new ViewDataDictionary(this.ViewData);
    childView.TemplateInfo.HtmlFieldPrefix = "Contact";
}
@Html.Partial("_PassengerContact", Model.Contact, childViewData)

You could also look at creating a Html.PartialFor overload yourself, as described in this stackoverflow question: ASP.NET MVC partial views: input name prefixes

Upvotes: 1

Johnny_D
Johnny_D

Reputation: 4652

Yes, indeed you can, but, controller usually works only with one model per request, so either your model should have declared within it properties of both partial submodels, or submodels themselves.

This is possible due to HTML specifications, all data on form, which has submit buttom is send to submit action url.

Upvotes: 1

Related Questions