【问题标题】:Pass multiple parameters in Html.BeginForm MVC4 controller action在 Html.BeginForm MVC4 控制器动作中传递多个参数
【发布时间】:2014-01-06 04:21:35
【问题描述】:

我有这样的事情:

   public ActionResult ImageReplace(int imgid,HttpPostedFileBase file)
    {
        string keyword = imgid.ToString();
        .......
    }

在我的 .cshtml 中:

   @model Models.MemberData
   @using (Html.BeginForm("ImageReplace", "Member", FormMethod.Post,
            new { imgid = @Model.Id, enctype = "multipart/form-data" }))
        { 
     <input type="file" name="file" id="file" value="Choose Photo"  /> 
     <input type="submit" name="submit" value="Submit" />
    }

这里 imgid 的值没有传递给控制器​​动作。显示错误,参数字典包含方法“System.Web.Mvc.ActionResult ImageReplace”的不可为空类型“System.Int32”的参数“imgid”的空条目

【问题讨论】:

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


    【解决方案1】:

    使用this overload,它可以让你区分路由值和HTML属性:

    @using (Html.BeginForm(
            "ImageReplace", "Member", 
            new { imgid = @Model.Id }, 
            FormMethod.Post,
            new { enctype = "multipart/form-data" }))
    { 
        <input type="file" name="file" id="file" value="Choose Photo"  /> 
        <input type="submit" name="submit" value="Submit" />
    }
    

    【讨论】:

      【解决方案2】:

      您还可以将imgid 作为表单中的字段传递,例如:

      @model Models.MemberData
      @using (Html.BeginForm("ImageReplace", "Member", FormMethod.Post,
              new { enctype = "multipart/form-data" }))
      { 
         @Html.HiddenFor(x => x.Id)
         <input type="file" name="file" id="file" value="Choose Photo"  /> 
         <input type="submit" name="submit" value="Submit" />
      }
      

      【讨论】:

        【解决方案3】:

        使用这个:

              @using (Html.BeginForm("ImageReplace", "Member",   
              new { imgid = @Model.Id },   FormMethod.Post,
          new { enctype = "multipart/form-data" }))
        

        【讨论】:

          【解决方案4】:
              @using (Html.BeginForm("Search", "Orders", FormMethod.Post, htmlAttributes: new { id = "example-form", @class = "app-search" }))
                 {
          
                     <input type="text" class="form-control" name="OrderNo" style="max-width:100% !important" placeholder="Search OrderNo"> 
                      <a class="srh-btn">
                       <i class="ti-close"></i>
                      </a>
                     </input>
          
                 }
          
            public ActionResult Search(FormCollection form)
                  {
                      string Orderno = form["OrderNo"];
                      int orderno = Convert.ToInt32(Orderno);
          
                      return View(orderno );
                  }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-03-04
            • 1970-01-01
            • 2010-11-10
            • 1970-01-01
            • 2021-02-09
            相关资源
            最近更新 更多