Click here to Skip to main content
15,900,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am working on a project which is on MVC, I have two buttons for a single form, on clicking first button,I am performing some action and showing a grid, a textbox and other button.
This is my View
C#
<pre lang="HTML">@using (Html.BeginForm("Index","Home",FormMethod.Post))
    {
	@Html.LabelFor(model => model.Name)
                @Html.TextBoxFor(model => model.Name , new { @class = "form-control" , @id = "txtbillingamount" })
	
	 @Html.LabelFor(model => model.Amount)
                @Html.TextBoxFor(model => model.Amount , new { @class = "form-control" , @id = "txtbillingamount" })
				
				  <button type="submit" name="command" value="request" >Next</button>
				  
				     @if (Model != null)
            {
               
                    <table class="table table-hover table-bordered table-striped">
                        <thead class="hbl-thead">
                            <tr>
                                <th> Title</th>
                                <th> Number</th>
                                <th>Amount</th>
                                
                            </tr>
                        </thead>
                        <tbody>
                            <tr>
                                <td>@Html.DisplayFor(model => model.ToAccountTitle)@Html.HiddenFor(model => model.ToAccountTitle)  </td>
                                <td>@Html.DisplayFor(model => model.RecieverMobileNumber)@Html.HiddenFor(model => model.RecieverMobileNumber)</td>
                                <td>@Html.DisplayFor(model => model.Amount)@Html.HiddenFor(model => model.Amount)</td>                                
                            </tr>
                        </tbody>
                    </table>
                
                <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">

                    <div class="form-group input-group-sm">
                        @Html.LabelFor(model => model.PIN)
                        @Html.PasswordFor(model => model.PIN , new { @class = "form-control" })                        
                    </div>
                    <button type="submit" name="command" value="payment">Submit</button>

                </div>
            }
	}

And this is my controller
C#
public ActionResult Index(MyModel model , string command){
  if (ModelState.IsValid)
          {
              if (command == "request")
              {
              //Return data in model to fill Table
              }
              else if (command == "payment")
              {

              }
              }

              return View(model);
}



The problem I am facing is,if ModelState.IsValid fails,its returning view with data filled in table, so it is showing the Pin text box and validating it on button Next click.

What I have tried:

I have to use post back on next button instead of ajax call
Posted
Updated 23-Aug-16 4:12am

1 solution

It is up to you to decide what you keep from the received model, what you take from the database and what you take the default value...

Typically one would display last values entered by the user when validation fails so that user would see what they have entered. However, for some fields, you don't want to show the old value. This would be the case for a password or a captcha for example where you should clear user value.

By the way, it is suspicious that both textboxes have the same id (txtbillingamount).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900