我的 View 中有 EditorFor
。
像这样
@Html.EditorFor(model => model.First().Link,
new { htmlAttributes = new { @class = "form-control", placeholder = "Email", id= "start" } })
还有我在 Controller 中的操作,从数据库中找到表中的所有 NULL 并用一些值更新它,这是代码
public ActionResult Update(string start="lol")
{
ApplicationDbContext context = new ApplicationDbContext();
IEnumerable<InvitationMails> customers = context.InvitationMails
.Where(c => c.Link == null)
.AsEnumerable()
.Select(c => {
c.Link = start;
return c;
});
foreach (InvitationMails customer in customers)
{
// Set that row is changed
context.Entry(customer).State = EntityState.Modified;
}
context.SaveChanges();
return RedirectToAction("Index");
}
在索引 View 中,我点击按钮进入更新操作并启动它 这是代码
<ul class="btn btn-default" style="width: 150px; list-style-type: none; font-size: 16px; margin-left: 20px">
<li style="color: white">@Html.ActionLink("Добавить почту", "Update", "InvitationMails", null, new { @style = "color:white" })</li>
</ul>
但这里是静态值更新,我想从 VIew 接收值。 我需要如何编写代码?
最佳答案
基本上,您应该设置呈现的input
的name
属性。
如果您使用 MVC 4,则在这种情况下您有 EditorFor
的空间重载。
你可以这样使用它:
@Html.EditorFor(model => model.First().Link,
null,
"start", //That will set id and name attributes to start
new { @class = "form-control", placeholder = "Email" })
请注意,您不再需要 id="start"
。
更新后:
您基本上有 2 个选择。
1s 选项 - 使用 form
:
@using (Html.BeginForm("Update", "InvitationMails", FormMethod.Post)) //maby you need GET
{
@Html.EditorFor(model => model.First().Link,
null,
"start", //That will set id and name attributes to start
new { @class = "form-control", placeholder = "Email" })
<ul class="btn btn-default" style="width: 150px; list-style-type: none; font-size: 16px; margin-left: 20px">
<li style="color: white">
<button type="submit" style="color:white">Добавить почту</button>
</li>
</ul>
}
第二个选项 - 在操作链接点击上使用 js。
关于c# - 将值从 EditorFor 传递到方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42713831/