MVC3将基类传递给部分视图-提交表单仅具有父类值 [英] MVC3 passing base class to partial View - submitting form only has parent class values

查看:79
本文介绍了MVC3将基类传递给部分视图-提交表单仅具有父类值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有许多子ViewModel类,它们是从基本ViewModel类继承的.

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

我将我的孩子ViewModel传递给我的View,然后将其自身传递给局部视图.主视图采用子类型,而局部视图采用父类型.

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.

当我手动填充属性时,所有内容都能正确显示.但是,当我提交表单时,我的控制器操作仅具有Child类的属性-所有基类属性都没有完成?

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?

例如

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;
        }
    }

使用View如此简单:

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" />
}

局部视图上只有一个文本框.

在IE中:ViewSource显示表单标签位于两个tetxboxes周围.

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已正确填充,但model.Name为空?

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.

推荐答案

无法复制.对我来说很好.请注意,我没有在视图模型中使用任何构造函数,因为模型绑定器将无法调用它们,并且所有属性都必须具有公共getter和setter.

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.

型号:

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

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

控制器:

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

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

查看(〜/Views/Home/Index.cshtml ):

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

部分(〜/Views/Home/Name.cshtml ):

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

当我在POST Index动作中提交表单时,模型属性Name和Visits都正确地与表单中的值绑定.

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

这篇关于MVC3将基类传递给部分视图-提交表单仅具有父类值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆