C# MVC4 中@Html.Textbox 的验证

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13024030/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 01:23:16  来源:igfitidea点击:

Validation of @Html.Textbox in MVC4

c#asp.netasp.net-mvcasp.net-mvc-3

提问by kk1076

I use an update action to update based on the input from the @Html.Textbox.

我使用更新操作根据来自@Html.Textbox 的输入进行更新。

@using (Html.BeginForm("Update", "Shopping", new { UserID = Request.QueryString["UserID"] }, FormMethod.Post, new { id = "myForm" }))
   {               
   @Html.ValidationSummary()                
   @Html.Hidden("id", @Request.QueryString["UserID"] as string)
   @Html.Hidden("productid", item.ProductID as string)
   @Html.TextBox("Quantity", item.Quantity)   
   @Html.ValidationMessage("Quantity", "*") 
   @Html.Hidden("unitrate", item.Rate)               
   <input type="submit" value="Update" />
   }

and In My Model class

和在我的模型类中

        [Required(ErrorMessage = "Quantity is required.")]
        [Display(Name = "Quantity")]
        [Range(2, 100, ErrorMessage = "There is not enough inventory for the product to fulfill your order.")]
        public int? Quantity { get; set; }

The problem is I m not getting the validation message when the textbox is empty. But when I use @Html.TextBoxFor

问题是当文本框为空时我没有收到验证消息。但是当我使用@Html.TextBoxFor

   @Html.TextBoxFor(modelItem => item.Quantity)
   @Html.ValidationMessageFor(modelitem => item.Quantity) 


I am getting the validation message. and my update action is not working.
Here I have two options.
1. How to pass the textbox name "qty" in @Html.TextboxFor ?? (or)
2. How to get the validation message in @Html.Textbox() using @Html.ValidationMessage()


我收到验证消息。并且我的更新操作不起作用。
在这里我有两个选择。
1. 如何在@Html.TextboxFor 中传递文本框名称“qty”?(或)
2.如何使用@Html.ValidationMessage()获取@Html.Textbox()中的验证消息

Any suggestions ..

有什么建议 ..

EDIT : My Update Action

编辑:我的更新操作

[HttpPost]
   public ActionResult Update(string id, string productid, int Quantity, decimal unitrate)
        {
        if (ModelState.IsValid)
         {
                   int _records = UpdatePrice(id, productid, Quantity, unitrate);
                    if (_records > 0)
                    {
                        return RedirectToAction("Index1", "Shopping", new { UserID = Request.QueryString["UserID"] });
                    }
                    else
                    {
                        ModelState.AddModelError("","Can Not Update");
                    }
                }
                return View("Index1");
            }

采纳答案by Rafay

you have the answer in your question, when you use

你在你的问题中有答案,当你使用

@Html.TextBoxFor(modelItem => item.Quantity)
@Html.ValidationMessageFor(modelitem => item.Quantity)

you get the error message becasue the MVC model validation works on the nameattributes as @Mystere Man said in the comments you are defying all the conventions and conventions is what MVC is all about, either change the property name in your model or use it as it is in the view if you want to leverage the MVC's model validation.

您会收到错误消息,因为 MVC 模型验证对name属性起作用,正如@Mystere Man 在评论中所说,您违反了所有约定,而约定就是 MVC 的全部内容,要么更改模型中的属性名称,要么直接使用它如果您想利用 MVC 的模型验证,则在视图中。



Not entirely relevant but a good read.

不完全相关,但很好读。