代码之家  ›  专栏  ›  技术社区  ›  Mister 832

在MVC页面中获取第二个表单的输入

  •  0
  • Mister 832  · 技术社区  · 8 年前

    我已经建立了一个页面,其中主控和详细信息(图片,aso)显示在一个页面上。但是,如果单击了第二个表单的submitt按钮,则如果第一个表单中的验证失败,则不会提交该表单。如果我更正这些值 HttpPostedFileBase uploadFile 为空。

    页面如下所示:

    @model app1.Models.MasterModel
    
    @{
        ViewBag.Title = "Edit";
        Layout = "~/Views/Shared/_Layout.cshtml";
    }
    
    @using (Html.BeginForm(new { @class = "form-inline col-lg-12" }))
    {
        @Html.AntiForgeryToken()
    
        <div>
            <h4>MasterModel</h4>
            <hr />
            @Html.ValidationSummary(true, "", new { @class = "text-danger" })
            @Html.HiddenFor(model => model.Id)
    
    
            <div class="row">
                @*Master properties*@
    
                <div class="col-md-4 col-lg-4">
                    <div class="form-horizontal">
                        <div class="form-group">
                            @Html.LabelFor(model => model.Title, htmlAttributes: new { @class = "control-label col-md-3" })
                            <div class="col-md-8">
                                @Html.EditorFor(model => model.Title, new { htmlAttributes = new { @class = "form-control" } })
                                @Html.ValidationMessageFor(model => model.Title, "", new { @class = "text-danger" })
                            </div>
                        </div>
    
                     @* aso... *@
    
                    </div>
                </div>
    
    
    }
    
    
                @*  Master Details *@
    
                <div class="col-md-4 col-lg-4">
    
    
    
                    @using (Html.BeginForm("NewPic", "Master", FormMethod.Post, new { enctype = "multipart/form-data" }))
                    {
                        <input name="uploadFile" type="file" />
                        <input type="submit" value="Upload File" /> <!-- First Button, does not work -->
    
                        <div class="container-fluid">
                            @foreach (app1.Models.PicModel b in Model.Pics)
                            {
    
                                var base64 = Convert.ToBase64String(b.DbPic);
                                var imgSrc = String.Format("data:image/gif;base64,{0}", base64);
    
    
                    <img src="@imgSrc" width="200" height="200" />
                            }
                        </div>
    
    
                        @Html.ActionLink("Upload", "NewPic", new { id = Model.Id }) <!-- Second Button, does not work either -->
                        <label class="control-label col-md-4 col-lg-4" for="Title">Picer</label>
                    }
                </div>
    
            </div>
    
            <div>
                <div class="form-group">
                    <div class="col-md-offset-2 col-md-12 col-lg-12">
                        <input type="submit" value="Save" class="btn btn-default" />
                    </div>
                </div>
            </div>
        </div>
    
    
    }
    
    <div>
        @Html.ActionLink("Back to List", "Index")
    </div>
    
    
    
    
    @section Scripts {
        @Scripts.Render("~/bundles/jqueryval")
    }
    

    控制器如下所示:

        public ActionResult NewPic(int id, HttpPostedFileBase uploadFile)
        {
             // uploadFile is null
        }
    
    1 回复  |  直到 8 年前
        1
  •  1
  •   Govinda Rajbhar    8 年前

    你忘了把 [HttpPost] 在NewPic方法之前。所以 新建PIC 方法将被视为[HttpGet],因此它将不起作用。

    [HttpPost]  
    public ActionResult NewPic(int id, HttpPostedFileBase uploadFile)
    {
         // uploadFile is null
    }
    

    并且还为这两个表单提供适当的Id,如下所示,以便在客户端验证时很容易使用这两个表单。

    表格1

    @using (Html.BeginForm(new {id = "Form1", @class = "form-inline col-lg-12" }))
    

    表格2

    @using (Html.BeginForm("NewPic", "Master", FormMethod.Post, new { id = "Form2", enctype = "multipart/form-data" }))
    

    有关更多信息,请访问 here