在MVC4 Razor中使用@ Html.Hidden进行空引用

时间:2013-06-25 07:49:03

标签: asp.net-mvc-4 razor

我有两个观点Create&编辑。 两者都有一个名为ModelType的隐藏字段,可以在我的模型绑定器中使用它来绑定所有子类。

此隐藏字段在“编辑”视图中正常工作,但在“创建”视图中没有。 我收到了null reference exception行:

@Html.Hidden("ModelType" , Model.GetType().AssemblyQualifiedName) 

在创建视图中。

请帮助解决这里的问题。

Edit.cshtml

@using PartyBiz.Models.Objects
@model Organization

@using (Html.BeginForm("Edit", "Organization", FormMethod.Post))
{
@Html.ValidationSummary(true)
<fieldset>
    <legend>Edit Organization</legend>
    <div class="editor-label">
        @Html.LabelFor(model => model.C) 
        @Html.TextBoxFor(model => model.C, new { @class = "txt"}) 
        @Html.ValidationMessageFor(model => model.C) 
    </div> <br />

    <div class="editor-label">
        @Html.LabelFor(model => model.N)
        @Html.TextBoxFor(model => model.N, new { @class = "txt"}) 
        @Html.ValidationMessageFor(model => model.N)
    </div> <br />
    <div class="editor-label">
        @Html.LabelFor(model => model.D)
        @Html.TextBoxFor(model => model.D, new { @class = "txt"}) 
        @Html.ValidationMessageFor(model => model.D)
    </div> 
    <br />
        @Html.HiddenFor(model=> model.PID)
        @Html.Hidden("ModelType" , Model.GetType().AssemblyQualifiedName)
        <input type="submit" value="Edit" />

</fieldset>
}  

Create.cshtml

@using PartyBiz.Models.Objects
@model Organization

@using (Html.BeginForm("Create", "Organization", FormMethod.Post))
{

@Html.ValidationSummary(true)
<fieldset>
    <legend>Create a New Organization</legend>

    <div class="editor-label">
        @Html.LabelFor(model => model.C) 
        @Html.TextBoxFor(model => model.C, new { @class = "txt"}) 
        @Html.ValidationMessageFor(model => model.C) 
    </div> <br />

    <div class="editor-label">
        @Html.LabelFor(model => model.N)
        @Html.TextBoxFor(model => model.N, new { @class = "txt"}) 
        @Html.ValidationMessageFor(model => model.N)
    </div> <br />

    <div class="editor-label">
        @Html.LabelFor(model => model.D)
        @Html.TextBoxFor(model => model.D, new { @class = "txt"}) 
        @Html.ValidationMessageFor(model => model.D)
    </div> 
    <br />
        <input type="submit" value="Create" />
        @Html.Hidden("ModelType" , Model.GetType().AssemblyQualifiedName)

</fieldset>
}  

模型绑定器

public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        if (bindingContext.ValueProvider.ContainsPrefix("ModelType"))
        {
            //get the model type
            var typeName = (string)bindingContext
                .ValueProvider
                .GetValue("ModelType")
                .ConvertTo(typeof(string));
            var modelType = Type.GetType(typeName);

            //tell the binder to use it
            bindingContext.ModelMetadata =
                ModelMetadataProviders
                .Current
                .GetMetadataForType(null, modelType);
        }
        return base.BindModel(controllerContext, bindingContext);
    }

1 个答案:

答案 0 :(得分:0)

我的猜测是你没有在Create方法中传递任何值作为模型。您的create方法可能如下所示:

public ActionResult Create()
{
    return View();
} 

这将导致Razor模板中的Model属性为null。要解决此问题,您可以传入模型类的默认实例:

public ActionResult Create()
{
    return View(new Organization());
}