将值从EditorFor传递给方法

用户名

我认为EditorFor像这样

@Html.EditorFor(model => model.First().Link, 
   new { htmlAttributes = new { @class = "form-control", placeholder = "Email", id= "start" } })

另外我在控制器中的操作,该操作从数据库中查找表中的所有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");
 }

在索引视图中,我点击“更新操作”按钮并启动它,这是代码

<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那里获得价值。我该如何编写代码?

特奥·范·科特

您基本上应该设置namerender的属性input

如果您使用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。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章