MVC3 passing base class to partial View - submitti

2019-05-15 22:52发布

问题:

I have a number of child ViewModel classes which inherit from an base ViewModel class.

I pass my child ViewModel into my View, which then passes itself into a partial view. The main view takes the child type, but the partial view takes the Parent type.

Everything displays correctly when I manually populate the properties. However, when I Submit the form, my controller action only has properties for the Child class - none of the base class properties are completed?

e.g.

public abstract class BaseDetails
    {
        public string Name { get; set; }

        public BaseDetails()
        { }

        public BaseDetails(string name)
        {
            Name = name;
        }
    }

    public class LocalDetails:BaseDetails
    {
        public int Visits { get; set; }

        public LocalDetails()
        { }

        public LocalDetails(int visits, string name)
            :base(name)
        {
            Visits = visits;
        }
    }

With the View as simple as:

@using (Html.BeginForm())
{
    @Html.TextBoxFor(m => m.Visits)
    <br />
    @Html.Partial("Name", Model)
    <input id="Submit1" type="submit" value="submit" />
}

The partial view has a single textbox on it.

In IE: ViewSource shows the form tag is around both tetxboxes.

However, when I submit to the controller method:

[HttpPost]
public ActionResult EditLocal (LocalDetails model)

model.Visits is correctly populated, but model.Name is null?

Any ideas? I added parameterless constructors to the classes because I got an exception upon submission if I didn't.

回答1:

Unable to reproduce. Works fine for me. Notice that I am not using any constructors with my view models because the model binder wouldn't be able to call them and that all the properties must to have public getters and setters.

Model:

public abstract class BaseDetails
{
    public string Name { get; set; }
}

public class LocalDetails : BaseDetails
{
    public int Visits { get; set; }
}

Controller:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View(new LocalDetails());
    }

    [HttpPost]
    public ActionResult Index(LocalDetails model)
    {
        return View(model);
    }
}

View (~/Views/Home/Index.cshtml):

@model LocalDetails
@using (Html.BeginForm())
{
    @Html.TextBoxFor(m => m.Visits)
    <br />
    @Html.Partial("Name", Model)
    <input type="submit" value="submit" />
}

Partial (~/Views/Home/Name.cshtml):

@model BaseDetails
@Html.TextBoxFor(x => x.Name)

When I submit the form inside the POST Index action both model properties Name and Visits are correctly bound with values from the form.