MVC3 passing base class to partial View - submitting form only has parent class values

耗尽温柔 提交于 2019-12-07 16:03:41

问题


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.



来源:https://stackoverflow.com/questions/6257355/mvc3-passing-base-class-to-partial-view-submitting-form-only-has-parent-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!