简体   繁体   中英

How to validate textboxes in ASP.NET MVC

I am new to ASP.NET MVC and am trying to validate a text-box. Basically, if user inputs less than 2 or a non number how can I get the error to display. Here's the tutorial I am trying to follow.

I have my code below.

Create View:

<%= Html.ValidationSummary()%>
<%= using (HtmlBeginForm()){%>
<div class="half-col">
    <label for="Amount">Amount:</label>
    <%= Html.TextBox("Amount")%>
    <%= Html.ValidationMessage("Amount", "*")%>
</div>

Create Controller:

[AcceptVerbs (HttpVerbs.Post)]
public ActionResult Create([Bind(Exclude ="ID")] Charity productToCreate)
{
    //Validation
    if (productToCreate.Amount < 2)
        ModelState.AddModelError("Amount, Greater than 2 please");

    return View(db.Donations.OrderByDescending(x => x.ID).Take(5).ToList());  //Display 5 recent records from table 
}

Model:

public class Charity
{
    public int ID { get; set; }
    public string DisplayName { get; set; }
    public DateTime Date { get; set; }
    public Double Amount { get; set; }
    public Double TaxBonus { get; set; }
    public String Comment { get; set; }
}

Error:

CS1501 No overload for method 'AddModelError' takes 1 CharitySite

You are adding the error to your modelstate incorrectly. You can read more about the ModelStateDictionary on the MSDN

AddModelError takes 2 parameters, so you would want:

ModelState.AddModelError("Amount", "Greater Than 2 Please.");

Having said that, you can use attributes to validate your model properties so you don't have to write all of that code by hand. Below is an example using the Range attribute. The RegularExpression attribute could also work. Here is an MSDN article containing information about the different types of attributes.

public class Charity
{
    public int ID { get; set; }
    public string DisplayName { get; set; }
    public DateTime Date { get; set; }

    [Range(2, Int32.MaxValue, ErrorMessage = "The value must be greater than 2")]
    public Double Amount { get; set; }
    public Double TaxBonus { get; set; }
    public String Comment { get; set; }
}

Also as a side note, the tutorial you are following is for MVC 1&2. Unless you HAVE to use / learn that. I would recommend following the tutorial for MVC 5 here .

Change this line:

ModelState.AddModelError("Amount, Greater than 2 please");

to:

ModelState.AddModelError("Amount ", "Amount, Greater than 2 please");

The first parameter is the member of the model being validated; it can be an empty string just to indicate an error not associated to a field. By specifying the Amount field, internally it uses that to highlight the erroring field (the control should have input-validation-error CSS class added to it) if you are using all of the client-side validation pieces.

ModelState.AddModelError接受2个参数,而不是1个。链接到MSDN ModelStateDictionary.AddModelError Method

ModelState.AddModelError("Amount", "Greater than 2 please");
if (productToCreate.Amount < 2)
    ModelState.AddModelError("Amount", "Greater than 2 please");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM