开发者

How to make a "custom create form" in asp mvc 2?

开发者 https://www.devze.com 2023-02-03 12:53 出处:网络
May be my question is going to be a little confusing, I\'m building a simple booking system with asp mvc 2 (I\'m a beginner) I\'ve generated the CRUD views with it\'s controller. When I go to ~/bookin

May be my question is going to be a little confusing, I'm building a simple booking system with asp mvc 2 (I'm a beginner) I've generated the CRUD views with it's controller. When I go to ~/bookings/create appears the autogenerated create form. But it appears with textfields, well I'm working on it changing them by some dropdown lists. The que开发者_如何转开发stion is here, how do I save the form with the selected values in all the dropdowns and the textfields?

Thank you! And sorry if It's kind of strange question, It's my first time on Stack Overflow.


You will need another action method adorned with [HttpPost] attribute and it will accept the same model object that you rendered as your form.

So suppose the following was your action method that you used to create your form:

public ActionResult Create(){
    var model = new Model();
    return View(model);
}

You will need to create another method like the following:

[HttpPost]
public ActionResult Create(Model model){
    if (ModelState.IsValid){
        //write the code to save the object here
        return RedirectToAction("Index); //This should be the where the user would go if the "Create" operation was successful
    }
    return View(model); //Else return the user to the same view and show any errors.
}
0

精彩评论

暂无评论...
验证码 换一张
取 消