Question

I am using MVC and have a view (test.cshtml) that contains a form. Is there a way to send it to another View page.cshtml for testing instead of same [http] controller ActionResult test()?

I am trying to validate that all the form field values are correct before updating db. Is there an easier way to do this?

Was it helpful?

Solution

In your view

@using (Html.BeginForm("Add", "Weight", FormMethod.Post))    
//where "Add" is the Action name and Weight is the controller (WeightController) -> http://foo/Weight
{
......
}

With a model

public class WeightModel
{
    [Required] public string Description { get; set; }
    [Required] public int Weight { get; set; }
    public string Notes { get; set; }
}

In your Controller

    [HttpPost]
    public ActionResult Add(WeightModel model)
    {
        if (!ModelState.IsValid) //framwork will validate based on attributes on model
        {
            return View("Index", model);
        }
        else
        {
            //save to db
            return RedirectToAction("Added");
        }
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top