将模型传递到局部视图不起作用

时间:2018-04-28 02:37:13

标签: asp.net-mvc

文件“ Add.cshtml ”正在调用部分视图“ _ManagePartial.cshtml ”并传递“模型”“但是“模型”未在局部视图中被选取。

当我将部分视图中的代码包含在“ Add.cshtml ”中时,一切都很有效。

请告知我做错了什么。

错误消息: Click here to see the error message screenshot

档案 :_ManagePartial.cshtml

<div class="row">
    <div class="col-md-4">
        <div class="form-group">
            @Html.LabelFor(model => model.UserName)
            @Html.EditorFor(model => model.UserName, new { htmlAttributes = new { @class = "form-control input-lg" } })
            @Html.ValidationMessageFor(model => model.UserName, "", new { @class = "text-danger" })
        </div>
        <div class="form-group">
            @Html.LabelFor(model => model.UserPassword)
            @Html.EditorFor(model => model.UserPassword, new { htmlAttributes = new { @class = "form-control input-lg" } })
            @Html.ValidationMessageFor(model => model.UserPassword, "", new { @class = "text-danger" })
        </div>
        <div class="form-group"><button type="submit" class="btn btn-primary btn-lg">Submit</button></div>
    </div>
    <div class="col-md-4">
        <div class="form-group">
            @Html.LabelFor(model => model.UserEmail)
            @Html.EditorFor(model => model.UserEmail, new { htmlAttributes = new { @class = "form-control input-lg" } })
            @Html.ValidationMessageFor(model => model.UserEmail, "", new { @class = "text-danger" })
        </div>
        <div class="form-group">
            @Html.LabelFor(model => model.UserConfirmPassword)
            <input type="password" class="form-control input-lg" name="UserConfirmPassword">
        </div>
    </div>
</div>

文件 :Add.cshtml

@model CardDistro.Models.AddUserViewModel
@{
    ViewBag.Current = "Users@Index";
    String Action = ViewBag.FormAction;
}

<h2 class="page-header">@ViewBag.Title</h2>
@using (Html.BeginForm(Action, "Users", FormMethod.Post, new { id = "SiteAddUserContainer" }))
{
    <input type="hidden" name="UserID" value="@Model.UserID" />
    if (ViewBag.Retval != null){
        <div class="row"><div class="col-md-8"><div class="alert alert-danger">@ViewBag.Retval</div></div></div>
    }
    Html.RenderPartial("_ManagePartial", Model);
}

1 个答案:

答案 0 :(得分:2)

_ManagePartial.cshtml需要通过

启动部分视图来识别模型

@model CardDistro.Models.AddUserViewModel

所以编译器知道如何处理model=>UserName