View values not returned to the controller action

Can anyone tell whats wrong with my view? my model values is displayed correctly in my view but it is not returned to the controller action. public ActionResult DeleteConfirmed(ClientModel contacts) my ClientModel is returned null.

View:

@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>
}

Controller Action:

[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");
        }

1. Keep the helpers inside the Form like below.

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

2. In order to Submit the form and send the model in Post Action Method, you have to use a Hidden Field along with each 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. You will not be able to see the Model values in Post Action Method when the View is like below..

@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" />
}

This is because you are using DisplayFor for all your properties. DisplayFor will not send anything in a POST, it doesn't create an input. You could add @Html.HiddenFor(model => model.CanaClie0012.Client00130012) , etc. to each one of your properties in order for your view to send anything back.

链接地址: http://www.djcxy.com/p/53964.html

上一篇: 发布ViewModel返回null属性

下一篇: 查看值不会返回到控制器操作