2

I have the following hidden fields inside my View:

@Html.HiddenFor(model => model.cat.Id)
@Html.HiddenFor(model => model.cat.OwnerId)
@Html.HiddenFor(model => model.cat.BirthDate        
@Html.HiddenFor(model => model.cat.Weight)

Id is a Key and OwnerId is a ForeignKey. When I try to edit that model, all values are displayed correctly but the problem is when I try to POST those values back. Id and OwnerId are always 0. I have a lot of other values inside my view like dropdown lists, text boxes etc, all those values are posted correctly. The problem is only with Id and OwnerId.

Controller:

[HttpPost]
public ActionResult Edit(EditCatDetailsViewModel model)
{
Debug.WriteLine("Cat Id: " + model.cat.Id); //displays 0
}

I checked source of web site and I see Id and OwnerId have correct values (values aren't 0). My question is why are those values not sent like all other values? Why are they received in controller as 0?

Thank you!

UPDATE:

Cat Edit View:

@model Pro.Web.Models.EditCatDetailsViewModel

@{
    ViewBag.Title = "Edit";
}

<h2>Edit Cat Details</h2>

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

<fieldset>
    <legend>Cat</legend>

    @Html.HiddenFor(model => model.cat.Id)
    @Html.HiddenFor(model => model.cat.OwnerId)
    @Html.HiddenFor(model => model.cat.BirthDate        
    @Html.HiddenFor(model => model.cat.Weight)

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

    <div class="editor-label">
        @Html.LabelFor(model => model.cat.Description)
    </div>
    <div class="editor-field">
        @Html.TextAreaFor(model => model.cat.Description, new { @style="width:400px; height:600px" })
        @Html.ValidationMessageFor(model => model.cat.Description)
    </div>

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

    <p>
        <input type="submit" value="Save" />
    </p>
</fieldset>
}

<div>
@Html.ActionLink("Back to Details", "Details")
</div>
4

2 回答 2

0

最后我设法做到了这样。我在代表猫 ID 的 EditCatDetailsViewModel 中放置了其他变量。

这是该模型的简化外观:

public class EditCatDetailsViewModel
{
    public Cat cat { get; set; }
    public int catId { get; set; }

}

在我的 GET Edit 方法中,我从 Cat 模型中的 Id 设置了该 Id:

// GET: /Cats/Edit/5

    public ActionResult Edit(int id = 0)
    {
        Cat cat = _catRepository.Find(id);

        if (cat == null)
        {
            return HttpNotFound();
        }

        EditCatDetailsViewModel model = new EditCatDetailsViewModel();
        model.cat = cat;
        model.catId = cat.Id;

        return View(model);
    }

在我的视图中,我添加了额外的隐藏元素:

@Html.HiddenFor(model => model.catId)

现在我可以在 POST 方法中访问该 ID:

[HttpPost]
public ActionResult Edit(EditCatDetailsViewModel model)
{
    Debug.WriteLine("Cat Id: " + model.catId); //displays correct Id for Cat!!!
    model.cat.Id = model.catId;
}

现在一切正常,我可以将更改保存到我的数据库中。我仍然对为什么没有第一次发布这些值感兴趣。

于 2013-05-06T07:01:03.907 回答
-1

嗨,似乎通过剃刀生成的隐藏字段不会在再次发布它们时更新它们的值。所以请尝试通过html syntex而不是像这样的剃刀来制作隐藏字段:

    <input type="hidden" id="" name="" value="@Model.model.cat.Id" />
    <input type="hidden" id="" name="" value="@Model.model.cat.OwnerId" />

检查您的页面源并从那里复制这些隐藏字段的名称属性的值并在此处分配它们,现在检查我希望您也能在您的操作方法中获得这些值。

于 2013-05-06T03:57:22.377 回答