c# - 将值从 EditorFor 传递到方法

标签 c# asp.net asp.net-mvc linq

我的 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 接收值。 我需要如何编写代码?

最佳答案

基本上,您应该设置呈现的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"

更新后:

您基本上有 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/

相关文章:

asp.net-mvc - MVC3 中的 SignalR、时序和启动/连接问题?

c# - automapper 可以生成 url 吗?

c# - 如何从字符串变量中提取 2 个日期?

ASP.Net 错误 : “The type ‘foo’ exists in both ”temp1. dll“和”temp2.​​dll“(第 2 部分)

c# - 去除不在安全列表中的 HTML 标签的方法

c# - 解决 ASP.NET MVC View 中的 "DataContext accessed after Dispose"错误

c# - ASP.NET MVC5 路由注释 MS_DirectRouteMatches

c# - Entity Framework 和泛型

c# - 使用点击动画以编程方式推进 Powerpoint 幻灯片放映

c# - 模板中的服务器控件如何对数据上下文敏感?