Multiple forms in ASP.NET MVC

前端 未结 5 2192
生来不讨喜
生来不讨喜 2020-12-20 16:46

Context
Let`s say i have:
In layout Site.Master:

相关标签:
5条回答
  • 2020-12-20 16:53

    If you are referring to a http post, only a post initiated (it can also be initiated by javascript) by a submit button from within the form will be posted to the server.

    If your forms are nested then this won't work. The outer form will always post to the server.

    In the sample HTML below, clicking on the submit button on the first form will not send the values from the second form to the server. Likewise, clicking the second submit button won't post the values from the first form.

    <html>
    ...
      <body> 
        <div>
    
          <form action="/Login/Login" method="post">
            <input type="text" name="username" value="" />
            <input type="text" name="passowrd" value="" />
            <input type="submit" name="login" value="Login" />
          </form>
    
    
          <form action="/Login/AdminLogin" method="post">
            <input type="text" name="username" value="" />
            <input type="text" name="passowrd" value="" />
            <input type="submit" name="login" value="Login Admin" />
          </form>
        </div>
    </body>
    </html>
    

    If you only wish to update/change one of the form section, then no this can not be done without using javascript and performing a javascript post(aka Ajax).

    0 讨论(0)
  • 2020-12-20 16:58

    If you have two simple forms, you can use this aproach:

    You create two different partial views.

    @model CustomerInfoModel
    @using (Ajax.BeginForm("CustomerInfo", "Customer", new AjaxOptions { HttpMethod = "Post", OnBegin = "InfoLoading", OnComplete = "InfoCompleted" }, new { id = "info", @class = "form-horizontal" }))
        {
        <input type="text" class="form-control" name="Name" id="Name" value="@Model.Name" />
        <input type="email" class="form-control" name="Email" id="Email"  value="@Model.Email" />
        <button type="submit" id="save-info" class="btn-medium red">Save</button>
        }
    

    and

    @model CustomerPasswordChangeModel
    @using (Ajax.BeginForm("CustomerPasswordChange", "Customer", new AjaxOptions { HttpMethod = "Post", OnBegin = "InfoLoading", OnComplete = "InfoCompleted" }, new { id = "change", @class = "form-horizontal" }))
    {
    <input type="password" class="form-control" name="OldPassword" id="OldPassword"  value="" />
    <input type="password" class="form-control" name="NewPassword" id="NewPassword"  value="" />
    <button type="submit" id="save-change" class="btn-medium red" autocomplete="off">Save</button>
    }
    

    In your parent view,

    @Html.Partial("CustomerInfo", Model.CustomerInfo)
    

    and

    @Html.Partial("CustomerPasswordChange", Model.CustomerPasswordChange)
    

    In Controller:

        [HttpPost]
        public ActionResult CustomerInfo([Bind(Include = "Name,Email")] CustomerInfoModel model)
        {
            if (ModelState.IsValid)
                return new Json(new { success=true, message="Updated.", errors=null);
    
    // do you logic
    
            return new Json(new { success=false, message="", errors=getHtmlContent(ModelState.Values.SelectMany(v => v.Errors).ToList(), "ModelError"));
        }
    
        [HttpPost]
        public ActionResult CustomerPasswordChange([Bind(Include = "OldPassword,NewPassword")] CustomerPasswordChangeModel model)
        {
            if (ModelState.IsValid)
                return new Json(new { success=true, message="Updated.", errors=null);
    
    // do you logic
    
            return new Json(new { success=false, message="", errors=getHtmlContent(ModelState.Values.SelectMany(v => v.Errors).ToList(), "ModelError"));
        }
    

    This will do what you want to do.

    Note: getHtmlContent method is just generating an error message to be displayed on page. Nothing so special. I may share it if required.

    0 讨论(0)
  • 2020-12-20 17:04

    If you build a controller method that accepts a FormCollection and your view has two forms defined, the formcollection returned will either be populated with values from form A or form B. You can inspect the formCollection and branch your logic based on the value therein. If you want the be very explicit you could have the same hidden variable occur in both forms with a value that would help your make your choice.

    That's one approach. there are a few ways to deal with this I'm sure.

    0 讨论(0)
  • 2020-12-20 17:08
    if(pass != true) 
    { 
     ViewData["Message'] = "Hey your login failed!"; Return View("Login") 
    }
    

    On ViewPage

    <form action="/tralala/Account/Login" method="post"> 
      <input name="name" />Name<br /> 
      <input name="password" type="password" />Password<br /> 
    
      <button>Login</button> 
     <div style="color: red"><%=ViewData["Message"] %><div> 
    </form>
    
    0 讨论(0)
  • 2020-12-20 17:12

    Your question is not very clear.

    But as far as I could understand, the answer is most likely yes. You can update anything you want depending on the user input.

    0 讨论(0)
提交回复
热议问题