2

I have this :

@using (Html.BeginForm("Action", "Controller", FormMethod.Post))
{
@Html.TextBoxFor(m => m.City, new { id = "tbCity", maxlength = 50 })
<input type="submit" value="Save" class="btSave" />
}

I'd like do a sublit make some validation (check if "tbCity" as a length != 0), I created a function with all validation, how can I call here ? and cancel if the validation is not correct ?

Thanks,

Update1

@Adam Pope : Imagine, I have a datalodel names "Person"s (firstname, lastname, ...), a model class with public Person Person{ get; set; }, where use the [Required] attribute ?

2 Answers 2

2

If you are using ASP.NET MVC 2 or 3 then you should checkout DataAnnotations

http://weblogs.asp.net/scottgu/archive/2010/01/15/asp-net-mvc-2-model-validation.aspx
http://www.asp.net/mvc/tutorials/creating-a-mvc-3-application-with-razor-and-unobtrusive-javascript

They let you specify your validation constraints on your Domain Model and have the UI automatically generate the validation code for you.

In this case you might have a model

public class MyModel
{
    [Required]
    public string City { get; set; }
}

then your view uses that model

@model MyModel

@Html.TextBoxFor(x => x.City)
@Html.ValidationMessageFor(x => x.City)

Update

For a drop-down, I tend to do the following, not 100% sure it's optimal but it works

public class MyModel
{ 
    [Range(1,int.MaxValue,ErrorMessage="Please select a value")]
    public int OtherModelID { get; set; }

    public IList<OtherModel> OtherModels {get; set; }
}

then in the view

@model MyModel

<select id="OtherModelID" name="OtherModelID">
    <option value="0">Please Select</option>
    @foreach(var m in Model.OtherModel)
    {
        <option value="m.ID">m.Name</option>
    }
</select>

You can put a condition in there to select the current value as well if you're on an edit screen.

Sign up to request clarification or add additional context in comments.

2 Comments

Nice, for a Dropdown, I'd like has contraint value selected != 0, possible ?
The [Required] attribute should go on the model class properties. So you'd have [Required] public string FirstName {get;set;}
1

If you are using jQuery, then you should be using a built-in framework like jQuery validation.

It might sound a little bit over-kill for your scenario but validation rules explode very easily as the project grows. So it's great to use a validation framework. jQuery validate plug-in is widely used and immensely useful.

EDIT:

Code:

$("form").live("submit", function () { 

if ($("#tbCity").val().length == 0) { 
alert("tbCity length should be greater than 0"); return false; 
}

});

2 Comments

I'd like something quick, easy and I know because not a lo of time
what do you do if they have javascript disabled? how would you define this functionality on the server side for .NET MVC?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.