samedi 25 avril 2015

EnumDropDownListFor Change ajax call action and the selected value assign to one field of database


I want to update VazList in my database for the record that it's enumdropdownlistfor item has been changed.I want that the selected value be assigned to VazList of that record.

@using (Html.BeginForm("Index", "My", FormMethod.Post)) { 
<p>
        @Html.ActionLink("Create New", "Create")
    </p>
<table class="table">
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.Name)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.LastName)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.NationalIdNumber)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.CellPhoneNumber)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.EmailAddress)
            </th>

            <th>
                @Html.DisplayNameFor(model => model.VahedList)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.VazList)
            </th>
            <th></th>
        </tr>
 @foreach (var item in Model)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.Name)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.LastName)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.NationalIdNumber)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.CellPhoneNumber)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.EmailAddress)
                </td>

                <td>
                    @Html.DisplayFor(modelItem => item.VahedList)
                </td>
                <td>
                    @Html.EnumDropDownListFor(modelItem=>item.VazList)
                    <input class="btn btn-default" type="submit" value="ثبت">
                </td>
                <td>
                    @Html.ActionLink("Edit", "Edit", new { id = item.Id }) |
                    @Html.ActionLink("Details", "Details", new { id = item.Id }) |
                    @Html.ActionLink("Delete", "Delete", new { id = item.Id })
                </td>
            </tr>
        }

    </table>
}
</body>
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script>
$("#item_VazList").change(function () {
    $.ajax({
        url: "@Url.Action("Indexa", "My")",
        datatype: "text",
        type: "POST",
        success: function (data) {
            alert("Successful Process");
        },
        error: function () {
            $("#testarea").html("ERROR");
        }
    });
});

And my Action Conroller:

 [HttpPost]
    public ActionResult Indexa(ViewModelPerson model, int? id)
    {

        DataBaseContext db=new DataBaseContext();
        EfPerson efp = db.People.Find(id);

        efp.VazList = model.VazList;
       db.People.Attach(efp);
       db.Entry(efp).State = EntityState.Modified;
        db.SaveChanges();

        // other changed properties

        return RedirectToAction("Sabt");
    }

I need that VazList be updated for the selected item. Sorry if my english is not ok. Please help me thank you.


Aucun commentaire:

Enregistrer un commentaire