【问题标题】:Pass value from EditorFor to method将值从 EditorFor 传递给方法
【发布时间】:2017-03-10 08:39:24
【问题描述】:

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

【问题讨论】:

  • 为什么你有一个模型,它是一个集合,并试图绑定到与你的模型无关的东西。目前尚不清楚您要达到的目标。为了绑定到您的参数,您的输入需要name="start"。由于您没有绑定任何东西,只需手动创建输入
  • 然后您显示了一个操作链接,该链接似乎正在对您的方法进行 GET,但这并没有传递您输入的值。

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


【解决方案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"

更新后:

你基本上有两个选择。

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。

【讨论】:

  • 试试你的代码。不工作。我现在将更新我的问题,也许我没有写清楚
  • @E.S 你想传递给控制器​​的start 值在哪里?
  • 我需要从 EditorFor 获取文本并将其传递给更新操作结果
  • @E.S 你不想只使用form 和提交按钮而不是操作链接吗?
  • 嗯,也许你是对的,那么对于表单我需要如何编写代码?我需要使用我的方法吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-07
  • 2017-03-11
  • 1970-01-01
  • 2016-10-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多