如何制作“自定义创建表单”在 asp mvc 2 中?

发布于 2024-10-11 08:39:14 字数 230 浏览 0 评论 0原文

可能我的问题会有点令人困惑,我正在使用 asp mvc 2 构建一个简单的预订系统(我是初学者),我已经使用它的控制器生成了 CRUD 视图。当我转到 ~/bookings/create 时,会出现自动生成的创建表单。但它与文本字段一起出现,我正在努力通过一些下拉列表来更改它们。问题是,如何使用所有下拉列表和文本字段中的选定值保存表单?

谢谢你!如果这是一个奇怪的问题,我很抱歉,这是我第一次使用 Stack Overflow。

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 question 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

记忆里有你的影子 2024-10-18 08:39:14

您将需要另一个带有 [HttpPost] 属性的操作方法,它将接受您呈现为表单的相同模型对象。

因此,假设以下是您用来创建表单的操作方法:

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

您将需要创建另一个方法,如下所示:

[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.
}

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.
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文