Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 当为空时,隐藏的ViewModel属性默认为0-应保持为空_Asp.net Mvc_Asp.net Mvc 3_Asp.net Mvc 4 - Fatal编程技术网

Asp.net mvc 当为空时,隐藏的ViewModel属性默认为0-应保持为空

Asp.net mvc 当为空时,隐藏的ViewModel属性默认为0-应保持为空,asp.net-mvc,asp.net-mvc-3,asp.net-mvc-4,Asp.net Mvc,Asp.net Mvc 3,Asp.net Mvc 4,获取控制器方法: public ActionResult Create(int? parentId) { var model = new CreatePersonViewModel(); // pull set from db var parent = _db.Persons.FirstOrDefault(s => s.PersonId == parentId); if (parent != null)

获取控制器方法:

    public ActionResult Create(int? parentId)
    {
        var model = new CreatePersonViewModel();

        // pull set from db
        var parent = _db.Persons.FirstOrDefault(s => s.PersonId == parentId);

        if (parent != null)
        {
            model.ParentId = parent.PersonId;
        }

        return View("Create", model);
    }
职位:

public ActionResult Create(CreatePersonViewModel viewModel) // viewModel.ParentId is 0 when it's passed to this method from the view, even though I didn't set it, and it's null coming from GET.  
        {
            //bla bla
        }
我的看法是:

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>CreatePersonViewModel</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.Name)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Name)
            @Html.ValidationMessageFor(model => model.Name)
        </div>

        @Html.HiddenFor(model => model.ParentId) // tried with second parameter of "null" as well

        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}
@使用(Html.BeginForm()){
@Html.ValidationSummary(true)
CreatePersonViewModel
@LabelFor(model=>model.Name)
@EditorFor(model=>model.Name)
@Html.ValidationMessageFor(model=>model.Name)
@Html.HiddenFor(model=>model.ParentId)//也尝试使用第二个参数“null”

}

你知道为什么视图在该属性中返回一个值0,而该值应该为null吗?我没有任何细节要添加到这里,但是表单验证要求更多的文本,所以就在这里

它总是空的吗?另外,请分享你的模型。我假设你的ViewModel上的ParentId是可以为空的?有趣的是,你分享了所有的东西,但是对于得到一个好的答案很重要的东西-模型。我是个白痴。忘了财产上的问号了。谢谢rob@user2062383,我们都去过