gpt4 book ai didi

javascript - EnumDropDownListFor 更改 ajax 调用操作并将所选值分配给数据库的一个字段

转载 作者:行者123 更新时间:2023-12-01 05:42:33 26 4
gpt4 key购买 nike

我想更新数据库中的 VazList,以记录其 enumdropdownlistfor 项目已更改。我希望将所选值分配给该记录的 VazList

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

还有我的 Action Controller :

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

我需要更新所选项目的 VazList
抱歉,如果我的英语不好。
请帮帮我谢谢。

最佳答案

假设item有一个 ID属性,将该行更新为:

@Html.EnumDropDownListFor(modelItem=>item.VazList, new { data_id = item.ID })

这将向 HTML 添加一个数据参数,如 <select data-id="id" > (假设您使用的是 MVC 3+)。

然后将 ajax 调用更新为:

$.ajax({
url: "@Url.Action("Indexa", "My")",
datatype: "text",
type: "POST",
data: { id : $(this).data("id") }, //<--new part, get the id
success: function (data) {
alert("Successful Process");
},
error: function () {
$("#testarea").html("ERROR");
}
});

关于javascript - EnumDropDownListFor 更改 ajax 调用操作并将所选值分配给数据库的一个字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29873952/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com