形成mvc3 razor partialview帖子

时间:2013-03-12 16:42:14

标签: asp.net-mvc-3 razor

我有一个与BasicInfoModel关联的partialView _BasicInfoPartView。 此部分视图包含一些输入和下拉列表:

public class BasicInfoModel 
{       public string Name { get; set; }
        public string selectedRubric { get; set; }
        public IEnumerable<SelectListItem> Rubrics { get; set; }
}

Partial _BasicInfoPartView.cshtml

@model ProjectZeroWebSite.Models.PleGroupModel
@Html.ListBoxFor(m => m.selectedRubric, @Model.Rubrics })
@Html.TextBoxFor(m => m.Name )

可以在任何页面的FORM中调用此部分视图。只需要在页面模型中添加BasicInfoModel。

public class RandomPageModel
{
        public basicInfoModel { get; set; }
        public string info2{ get; set; }
        ...
        public RandomPageModel()
        { this.basicInfoModel = new basicInfoModel ();}
}

RandomPage.cshtml

@using (Html.BeginForm())
{
    @Html.Partial("_BasicInfoPartView ", @Model.BasicInfoModel)
    @Html.TextBoxFor(m => m.info2)
    ...
    <input type="submit"  />
}

现在问题: 我可以毫无问题地填充页面。 但是当我尝试从模型中检索控制器中的信息时: RandomPageModel.info2好的! RandomPageModel.BasicInfoModel为空...

我想我不理解数据绑定概念:s 我试试这个是因为我不想通过小JS函数重载我的页面而不喜欢复制我的表单(代码维护)。

先谢谢。

Cantinos。

1 个答案:

答案 0 :(得分:4)

在这种情况下,您需要使用编辑器模板而不是局部视图。

所以你定义了一个editorTemplate(强类型)

@model BasicInfoModel

@Html.ListBoxFor(m => m.selectedRubric, @Model.Rubrics })
@Html.TextBoxFor(m => m.Name )

将此编辑器模板命名为与模型相同,并将其放在共享视图文件夹中名为EditorTemplates的文件夹中

  

〜/查看/共享/ EditorTemplates / BasicInfoModel.cshtml

然后换掉这一行

@Html.Partial("_BasicInfoPartView ", @Model.BasicInfoModel)

这个

@Html.EditorFor(m => m.BasicInfoModel)