【问题标题】:Formcollection doesn't extract data from form MVCFormcollection 不会从表单 MVC 中提取数据
【发布时间】:2013-01-18 14:17:22
【问题描述】:

我遇到了表单收集问题。

我看到了一些表单,两个提交按钮都具有唯一的 name=""。在调试时,我在 formcollection 中的控制器上获取除提交按钮的“名称”之外的所有数据......我不知道为什么,因为我在其他表单中使用它并且效果很好。因此,我查看了代码,但是在不工作的formcol和工作的formcol上使用formcollection时找不到任何区别。我尝试重命名按钮,移动它们,添加我认为可以帮助的引用......没有。在每次提交时跳过我的条件,因为它只给我返回“false”,而我在 onclick 上的“上传”或“保存”按钮不包含...

所以我想请你帮忙。你能告诉我哪里可能出错了吗?谢谢大家!

这是在控制器中:

[HttpPost]
public ActionResult EditUser(EditUserVM model, int id, FormCollection c)
{
    //c["upload"] is everytime set to null, 'cos c does't contain it 
    if (c["upload"] != null)
    {
            //.... some code
            return RedirectToAction("Index", "Home");
    }

    if (ModelState.IsValid)
    {
           //.... next code 
    }

    return View("EditUser", model);
}

这是在视图中:

    @model PrukazOnline.ViewModels.EditUserVM
@{
    ViewBag.Title = "EditUser";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<script type="text/javascript" src="@Url.Content("~/Scripts/jquery.validate.min.js")"></script>
<script type="text/javascript" src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")"></script>
<script type="text/javascript" src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")"></script>
@using (Html.BeginForm("EditUser", "User", null, FormMethod.Post, new { @class = "form-horizontal", id = "formstep", enctype = "multipart/form-data" }))
{
    @*Here is some code - @Html.TextBoxFor(x => x.something) and @Html.ValidationMessageFor(x => x.something) - all of these are in formcollection*@   
    .
    .
    .
    .
    <div>
        <input type="submit" value="Nahrát" class="upl" name="upload" id="upload" />
    </div>

    <div class="submit_buttons">
            <input type="submit" name="save" id="save" value="Uložit" />
    </div>
}

【问题讨论】:

    标签: asp.net-mvc-3 formcollection


    【解决方案1】:

    问题在于单个表单中的多个提交输入。只会在FormCollection 中单击按钮。
    我想您尝试根据单击的按钮来改变表单处理,在这种情况下,最好为每个按钮使用不同的操作,如下所示:

    查看:

    <div>
        <input type="submit" value="Nahrát" class="upl" name="upload" id="upload" />
    </div>
    
    <div class="submit_buttons">
            <input type="submit" name="save" id="save" value="Uložit" />
    </div>
    

    控制器:

    [HttpParamAction]
    [HttpPost]
    public ActionResult Upload(EditUserVM model)
    {
        ...
    }
    
    [HttpParamAction]
    [HttpPost]
    public ActionResult Save(EditUserVM model)
    {
        ...
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-05-05
      • 2020-05-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多