【问题标题】:Multiple forms in one view page一个视图页面中的多个表单
【发布时间】:2020-06-24 02:41:14
【问题描述】:

我正在开发一个处理员工个人资料的应用程序。

我在一个视图页面中有 3 个表单(都在同一个控制器中),但它只保存第一个表单。当我保存第二种形式时,它会清除第一种和第三种形式的值。

这是我的代码:

观看次数/EMP/索引:

@using (Html.BeginForm("Edit", "EMPs", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    <div class="form-group">
        <div class="pull-right">
            <input type="submit" value="Update" name="personalsubmit" class="btn btn-success" />
        </div>
    </div>
}

@{ Html.RenderAction("Index", "EMP_REFERENCE", new { id = Model.eMP.lineno });}

@using (Html.BeginForm("Edit", "EMPs", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    <div class="form-group">
        <div class="pull-right">
            <input type="submit" value="Update" name="jobsubmit" class="btn btn-success" />
        </div>
    </div>
}

 @{ Html.RenderAction("Index", "EMP_BENEFITS", new { id = Model.eMP.lineno });}

@using (Html.BeginForm("Edit", "EMPs", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    <div class="form-group">
        <div class="pull-right">
            <input type="submit" value="Update" name="otherssubmit" class="btn btn-success" />
        </div>
    </div>
}

EMPsController

 public ActionResult Edit([Bind(Include = "lineno,EMPNO,IDNO..")] EMP eMP)
        {
            if (ModelState.IsValid)
            {
                if (Request.Form["personalsubmit"] != null)
                {
                    db.Entry(eMP).State = EntityState.Modified;
                    db.SaveChanges();

                }
                if (Request.Form["jobsubmit"] != null)
                {
                    db.Entry(eMP).State = EntityState.Modified;
                    db.SaveChanges();

                }
                if (Request.Form["otherssubmit"] != null)
                {
                    db.Entry(eMP).State = EntityState.Modified;
                    db.SaveChanges();

                }

                return Redirect(Request.UrlReferrer.PathAndQuery);
            }
            return View(eMP);
       }

我无法将它们全部放在一个表单中,因为我在它们之间使用了 ajax beginForm 来实现另一种 crud 方法。因为我读过不推荐使用嵌套表单。

有没有办法保存一个表单而不清除其他表单的值?

【问题讨论】:

    标签: asp.net-mvc multiple-forms


    【解决方案1】:

    有没有办法保存一个表单而不清除其他表单的值?

    1. 您可以简单地对每个表单使用 ajax.beginform; How to use Simple Ajax Beginform in Asp.net MVC 4?

    2. 或者您可以自己实现 ajax https://www.c-sharpcorner.com/blogs/using-ajax-in-asp-net-mvc

    3. 或者您可以直接将所有内容绑定到您的模型上;

    // don't need to specify which properties to bind, all of the available properties in your view will be bound to the model on POST
    public ActionResult Edit(EMP eMP)
    {
       if(eMP.FirstName!=null ...){
          // ... do some checking depending on what values are submitted
       }
       // save profile here
       // when you return the view
       return View(eMP);
    }
    

    但是对于这种方法,您只需要为 Personal、Job 和 Others 提供 1 个表单。

    
    @{ Html.RenderAction("Index", "EMP_REFERENCE", new { id = Model.eMP.lineno });}
    
    @{ Html.RenderAction("Index", "EMP_BENEFITS", new { id = Model.eMP.lineno });}
    
    @using (Html.BeginForm("Edit", "EMPs", FormMethod.Post, new { enctype = "multipart/form-data" }))
    {
        <!--put all your employee input fields here-->
        <div class="form-group">
            <div class="pull-right">
                <input type="submit" value="Update" name="submit" class="btn btn-success" />
            </div>
        </div>
    
       <!--put all your job input fields here-->
       <div class="form-group">
            <div class="pull-right">
                <input type="submit" value="Update" name="submit" class="btn btn-success" />
            </div>
        </div>
    
        <!--put all your others input fields here-->
        <div class="form-group">
            <div class="pull-right">
                <input type="submit" value="Update" name="submit" class="btn btn-success" />
            </div>
        </div>
    }
    

    【讨论】:

    • 感谢您的建议,但我不能只对 Personal、Job 和 Others 使用一种表单,因为这些表单之间的 RenderAction 是 ajax beginForm(它将是嵌套表单),此外,它会重新排列他们的订单类别
    • @AthenaLouisePaz 我能想到的唯一方法是 1 和 2,通过 ajax。
    • @Athena 酷,祝你好运。如果您遇到障碍,请务必包含您的完整视图代码 (html)。
    猜你喜欢
    • 2020-09-16
    • 1970-01-01
    • 2021-10-22
    • 2018-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多