1
votes

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");
        }
2
In addition to the below mentioned answer - Did you try like this @using (Html.BeginForm("ActionName", "ControllerName", FormMethod.Post, null )) - wuhcwdc
@PankajGarg that's a good suggestion, but I don't think it should be necessary, been using @using (Html.BeginForm()) forever and it works. I think there might be something else going on. - SOfanatic
hi pankaj, model is still returned null. I cant see what im missing here. - Romeo
@rcadaoas : Try placing the complete FieldSet section inside the BeginForm - wuhcwdc
Can you show me how to do that? @PankajGarg - Romeo

2 Answers

3
votes

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" />
}
1
votes

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.