从EditorFor中传递值到方法

6

我在我的视图中使用了 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>

但是这里的更新是使用静态值,我想从视图中接收值。我应该如何编写我的代码?


1
为什么你有一个模型是一个集合,却试图绑定到与你的模型毫不相关的东西。你试图实现什么还不清楚。为了绑定到你的参数,你的输入需要 name="start"。既然你没有绑定任何东西,就手动创建输入吧。 - user3559349
1
然后,您显示了一个操作链接,它似乎正在对您的方法进行GET请求,但是它没有传递您输入的值。 - user3559349
1个回答

1

您需要设置渲染的inputname属性。

如果您使用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"更新后: 您基本上有两个选项。
第一种选择-使用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。

尝试你的代码,但它没有运行。我现在会更新我的问题,也许我没有清楚地表达它。 - user7629010
@E.S,你的想要传递给控制器的“start”值在哪里? - teo van kot
我需要从EditorFor中获取文本并将其传递给Update Action结果。 - user7629010
@E.S,你不想使用带有提交按钮的 form 代替动作链接吗? - teo van kot
嗯,也许你是对的,那么关于表单,我需要如何编写代码呢?我需要使用我的方法吗? - user7629010

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接