tags:

views:

102

answers:

4

Edit.aspx:

<th>Calendar<input id="datepicker" name="datepicker" type="text" class="input-box"/></th>

Controller action:

    // POST: /Studenti/Edit/5

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Edit(int[] rb, int id, string datepicker)
    {
        List<nastava_prisustvo> nastava = new List<nastava_prisustvo>();
        if (String.IsNullOrEmpty(datepicker))
            ModelState.AddModelError("datepicker", "First name is required");
        try
        {
            if (ModelState.IsValid)
            {
                string poruka = "";
                for (int i = 1; i <= rb.Length; i++)
                {
                    string name = "chk" + i;
                    string selID = Request.Form[name];

                    if (selID == "on")
                    {

                        nastava.Add(new nastava_prisustvo
                        {
                            br_indexa = int.Parse(Request.Form["id_stud" + i]),
                            id_predmet = id,
                            datum = System.DateTime.Parse(Request.Form["datepicker"])
                        });
                    }
                }
                return View("show", nastava);
            }
        }
        catch(Exception ex){

            ModelState.AddModelError("*", "An unexpected error occurred.");
        }
        return View("show", nastava);
        }

    }

How to validate datepicker fiel? How stop posting data if date is not selected and show appropriate message. I use ASP>NET MVC 1 and read this http://www.superexpert.com/Blog/archive/2008/09/09/asp-net-mvc-tip-42-use-the-validation-application-block.aspx but did not solve my problem

+1  A: 

If you want to validate the date before posting back you're going to have to use client side validation. xVal is a popular plugin for this.

For server side validation you're going to want to look at model binding and model validation.

Jace Rhea
+1  A: 

ASP.NET MVC does not use the same sort of controls you're used to using in ASP.NET. Basically you have to do most things manually in JavaScript code, whereas in ASP.NET it generates that code automatically.

I'd use JQuery or something to validate the data in the control on the button click.

James
What then do Data Annotation Validators:http://www.superexpert.com/blog/archive/2008/09/10/asp-net-mvc-tip-43-use-data-annotation-validators.aspx
Ognjen
I would not rely just on Javascript to validate your date. You'll want server side validation as well. There are a lot of ways to do this in MVC. Ryan B has posted 1 way.
metanaito
A: 

since you have validate on the client with your own Javascript, try the jQuery UI data picker: http://jqueryui.com/demos/datepicker/

Then, most simply, on the server side, you can take the string value "datepicker" Convert it in a try/catch.

var dtDatePicker = new DateTime();
try
{
  dtDatePicker = Convert.ToDateTime(datepicker);
  // worked, carry on
}
catch
{
  // didn't work, invalid date.
}
craigmoliver
+1  A: 

I would stick to server side validation. Try this:

DateTime datePosted;
if (!DateTime.TryParse(Request.Form["datepicker"], out datePosted))
{
    this.ModelState.AddModelError("datepicker", "Invalid Date!");
}

if (this.ModelState.IsValid)
{
    return View("show", nastava);
}
else
{
    // return your GET edit action here
    return Edit(5);
}

Your Edit view will automatically be passed any validation errors and you can display them with a validation summary.

<%= Html.ValidationSummary() %>
Ryan B
How post this error on View page or this code automaticaly add error on page
Ognjen
I updated my code but this code does not write message in View page when user not set date
Ognjen
I'd refactor your method by getting rid of the try catch block, using TryParse methods to get and validate the int and date values, and only checking the ModelState.IsValid after everything is done but before your return your view.
Ryan B
I solved problem, thanks very much
Ognjen