1

谁能告诉我的观点有什么问题?我的模型值在我的视图中正确显示,但未返回到控制器操作。public ActionResult DeleteConfirmed(ClientModel contacts) 我的 ClientModel 返回 null。

看法:

@model ContactManager.Models.ClientModel

@{
    ViewBag.Title = "Delete Information";
}

<h2>Delete Information</h2>

<h3>Are you sure you want to delete this?</h3>
<fieldset>
    <legend>Details</legend>

    <div class="display-label">ID</div>
    <div class="display-field">
        @Html.DisplayFor(model => model.CanaClie0012.Client00130012)
    </div>

    <div class="display-label">Name</div>
    <div class="display-field">
        @Html.DisplayFor(model => model.Clientes0013.Nombre0013)
    </div>

    <div class="display-label">Contact Number</div>
    <div class="display-field">
        @Html.DisplayFor(model => model.CanaClie0012.Direcc0012)
    </div>

    <div class="display-label">Country</div>
    <div class="display-field">
        @Html.DisplayFor(model => model.CanaClie0012.F1Pais00200012)
    </div>

</fieldset>
@using (Html.BeginForm()) {
    <p>
        <input type="submit" value="Delete" /> |
        @Html.ActionLink("Back to List", "Index")
    </p>
}

控制器动作:

[HttpPost, ActionName("Delete")]
public ActionResult DeleteConfirmed(ClientModel contacts)
        {
            contacts.Clientes0013.Client0013 = contacts.CanaClie0012.Client00130012;
            contacts.Clientes0013.F1Pais00200013 = contacts.CanaClie0012.F1Pais00200012;
            Clientes0013 clientes0013 = db.Clientes0013.Find(contacts.Clientes0013.Client0013, contacts.Clientes0013.F1Pais00200013);
            db.Clientes0013.Remove(clientes0013);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
4

2 回答 2

3

1. 将助手保存在表单中,如下所示。

@using (Html.BeginForm("ActionName", "ControllerName", FormMethod.Post, null))
{
    @Html.DisplayFor(i => i.PropertyName);
    <input type="submit" name="Submit" value="Submit" />
}

2. 为了提交表单并在 Post Action Method 中发送模型,您必须使用一个隐藏字段以及每个 DisplayFor Helper。

@using (Html.BeginForm("ActionName", "ControllerName", FormMethod.Post, null))
{
    @Html.HiddenFor(i => i.PropertyName);
    @Html.DisplayFor(i => i.PropertyName);
    <input type="submit" name="Submit" value="Submit" />
}

3. 当视图如下图时,您将无法在 Post Action Method 中看到 Model 值。

@Html.HiddenFor(i => i.PropertyName);
@Html.DisplayFor(i => i.PropertyName);
@using (Html.BeginForm("ActionName", "ControllerName", FormMethod.Post, null))
{

    <input type="submit" name="Submit" value="Submit" />
}
于 2013-06-14T19:06:25.457 回答
1

这是因为您正在使用DisplayFor所有属性。DisplayFor不会在 POST 中发送任何内容,也不会创建输入。您可以将@Html.HiddenFor(model => model.CanaClie0012.Client00130012)等添加到您的每个属性中,以便您的视图发送回任何内容。

于 2013-06-14T17:56:06.403 回答