Mvc禁用某些控件的验证 [英] Mvc disable validation of some controls

查看:77
本文介绍了Mvc禁用某些控件的验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个MVC上的项目,我有两个按钮用于单个表单,单击第一个按钮,我正在执行一些操作并显示网格,文本框和其他按钮。

这是我的观点

< pre lang =   HTML> @using(Html.BeginForm( 索引,< span class =code-string>  Home,FormMethod.Post))
{
@ Html.LabelFor( model = > model.Name)
@ Html.TextBoxFor(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 = 提交 name = command value = request >下一步< / 按钮 >

@if(模型!= null
{

< table class = table table-hover table-bordered table-striped >
< thead class = hbl-thead >
< tr>
< th>标题< / th >
< th>编号< / th >
< th>金额< / 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)< / < span class =code-leadattribute> 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 = 付款>提交< / 按钮 >

< / DIV >
}
}



这是我的控制器

  public  ActionResult Index(MyModel model, string  command){ 
if (ModelState.IsValid)
{
if (command == request
{
// 返回模型中的数据以填充表
}
else if (command == payment
{

}
}

return 查看(模型);
}







我面临的问题是,如果ModelState.IsValid失败,它的返回视图,数据填写在表中,因此它显示Pin文本框并在按钮上验证它下一步点击。



我尝试过:



我必须在下一个按钮上使用回发而不是ajax调用

解决方案

它是由您决定从收到的模型中保留的内容,从数据库中获取的内容以及您采用的默认值...



通常会显示最后一个验证失败时用户输入的值,以便用户可以看到他们输入的内容。但是,对于某些字段,您不希望显示旧值。这就是密码或验证码的情况,例如你应该清除用户价值。



顺便说一下,两个文本框都有相同的id是可疑的( txtbillingamount )。

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

<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

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

解决方案

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).


这篇关于Mvc禁用某些控件的验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆