locked
2 submit buttons but only want to validate on one RRS feed

  • Question

  • User-327445417 posted

    I have 2 submit buttons.  First one is to Save and the second one is to Save and Post.  When Save is clicked I don't want to do any validation.  But when Save and Post is clicked I want to validate and make sure all fields are filled in.  This is in an MVC C# project. So being new to MVC C# I'm wondering what is the easiest way to do this and does anyone have any examples of how to do this?  Thank you!

    Friday, September 16, 2016 8:08 PM

Answers

  • User-2057865890 posted

    Hi Steedal,

    Model

    public class UserModel
    {
        public string userName { get; set; }
        public string password { get; set; }
    }

    View

    @model YourProjectNamespace.Models.UserModel
    @{
        Layout = null;
    }
    
    <!DOCTYPE html>
    
    <html>
    <head>
        <meta name="viewport" content="width=device-width" />
        <title>SaveUserInfo</title>
    </head>
    <body>
        @using (Html.BeginForm("SaveUserInfo", "Demo", FormMethod.Post))
        {
            <div>
                <table>
                    <tr>
                        <td>
                            UserName
                        </td>
                        <td>
                            :
                        </td>
                        <td>
                            @Html.TextBoxFor(m => m.userName)
                        </td>
                    </tr>
                    <tr>
                        <td>
                            Password
                        </td>
                        <td>
                            :
                        </td>
                        <td>
                            @Html.TextBoxFor(m => m.password)
                        </td>
                    </tr>
                </table>
                <br />
                <div style="padding-left: 80px;">
                    <input type="submit" id="Save" value="Save" name="Command" title="Save" />
                    <input type="submit" id="Save and Post" value="Save and Post" name="Command" title="Save and Post" />
                </div>
                <div>
                    @ViewBag.msg
                    <br />
                    @ViewBag.validate
                </div>
            </div>
        }
    </body>
    </html>
    

    Controller

    public class DemoController : Controller
    {
       public ActionResult SaveUserInfo()
       {
          return View();
       }
    
       [HttpPost]
       public ActionResult SaveUserInfo(UserModel user, string command)
       {
           if (command == "Save")
           {
               ViewBag.msg = "Save";
           }
           else if (command == "Save and Post")
           {
               var sb = new StringBuilder();
               if (string.IsNullOrEmpty(user.userName))
               {
                  sb.Append(" userName is required!");
               }
               if (string.IsNullOrEmpty(user.password))
               {
                  sb.Append(" password is required!");
               }
    
               ViewBag.msg = "Save and Post";
               ViewBag.validate = sb.ToString();
           }
                return View(user);
       }
    }

    Best Regards,

    Chris

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, September 20, 2016 7:06 AM

All replies

  • User-2057865890 posted

    Hi Steedal,

    Model

    public class UserModel
    {
        public string userName { get; set; }
        public string password { get; set; }
    }

    View

    @model YourProjectNamespace.Models.UserModel
    @{
        Layout = null;
    }
    
    <!DOCTYPE html>
    
    <html>
    <head>
        <meta name="viewport" content="width=device-width" />
        <title>SaveUserInfo</title>
    </head>
    <body>
        @using (Html.BeginForm("SaveUserInfo", "Demo", FormMethod.Post))
        {
            <div>
                <table>
                    <tr>
                        <td>
                            UserName
                        </td>
                        <td>
                            :
                        </td>
                        <td>
                            @Html.TextBoxFor(m => m.userName)
                        </td>
                    </tr>
                    <tr>
                        <td>
                            Password
                        </td>
                        <td>
                            :
                        </td>
                        <td>
                            @Html.TextBoxFor(m => m.password)
                        </td>
                    </tr>
                </table>
                <br />
                <div style="padding-left: 80px;">
                    <input type="submit" id="Save" value="Save" name="Command" title="Save" />
                    <input type="submit" id="Save and Post" value="Save and Post" name="Command" title="Save and Post" />
                </div>
                <div>
                    @ViewBag.msg
                    <br />
                    @ViewBag.validate
                </div>
            </div>
        }
    </body>
    </html>
    

    Controller

    public class DemoController : Controller
    {
       public ActionResult SaveUserInfo()
       {
          return View();
       }
    
       [HttpPost]
       public ActionResult SaveUserInfo(UserModel user, string command)
       {
           if (command == "Save")
           {
               ViewBag.msg = "Save";
           }
           else if (command == "Save and Post")
           {
               var sb = new StringBuilder();
               if (string.IsNullOrEmpty(user.userName))
               {
                  sb.Append(" userName is required!");
               }
               if (string.IsNullOrEmpty(user.password))
               {
                  sb.Append(" password is required!");
               }
    
               ViewBag.msg = "Save and Post";
               ViewBag.validate = sb.ToString();
           }
                return View(user);
       }
    }

    Best Regards,

    Chris

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, September 20, 2016 7:06 AM
  • User-105835280 posted

    you have working link 

    http://www.aspdotnet-suresh.com/2011/12/how-to-create-simple-login-form-using.html

    Tuesday, September 20, 2016 7:20 AM
  • User-327445417 posted

    This is great!  Thank you very much!!

    Tuesday, September 20, 2016 6:44 PM