它是关于使用 MVC 2.0 的 ASP.NET 应用程序 artitecture

发布于 2024-12-29 11:40:23 字数 122 浏览 2 评论 0原文

目前我正在开发 asp.net MVC-2 应用程序。我遇到了一个问题,如何将注册表单数据获取到控制器类中。有一个解决方案,我们可以使用 FormCollection 对象,但如何获取各个字段值。

有人可以帮忙吗?

Currently i am working on asp.net MVC-2 applications. i met with a problem that how to get signup form data into controller class. There is a solution that we can use FormCollection object but how to get individual field values.

Can anyone please help?

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

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

发布评论

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

评论(2

禾厶谷欠 2025-01-05 11:40:23

处理发布值的最简单方法是使用您提到的FormCollection对象。您可以像数组一样访问它:

public ActionResult YourAction(FormCollection form)
{
    // assuming a form element posted with the name, "user"
    var user = FormCollection["user"];
    return View();
}

处理发布值的最佳方法是使用强类型视图模型。视图模型将包含表单的属性。如果可能的话,MVC 框架会自动将您的表单元素绑定到该对象。

因此,您的视图模型类可能如下所示:

public class UserFormViewModel
{
    public string Username { get; set; }
    public int Age { get; set; }
}

如果您的 HTML 表单包含两个输入,其 name 属性设置为 UsernameAge,那么您的可以修改控制器操作以使用刚刚描述的强类型视图模型:

public ActionResult UserForm(UserFormViewModel vm)
{
    string username = vm.Username;
    int age = vm.Age;

    // persist to database, etc
    return View();
}

The simplest way to handle posted values is with the FormCollection object you mentioned. You can access it like an array:

public ActionResult YourAction(FormCollection form)
{
    // assuming a form element posted with the name, "user"
    var user = FormCollection["user"];
    return View();
}

The best way to handle posted values is by using a strongly typed view model. The view model would contain properties of your form. The MVC framework will automagically bind your form elements to this object, if possible.

So, your view model class might look like:

public class UserFormViewModel
{
    public string Username { get; set; }
    public int Age { get; set; }
}

If your HTML form contained two inputs with their name attributes set to Username and Age, then your controller action could be modified to use the strongly types view model just described:

public ActionResult UserForm(UserFormViewModel vm)
{
    string username = vm.Username;
    int age = vm.Age;

    // persist to database, etc
    return View();
}
﹂绝世的画 2025-01-05 11:40:23

去下载以下项目。有一个免费的 pdf 文档。完成整个样本。 了解的所有基础知识

它使您了解有关 ASP.NET MVC 框架Mvc Music Store

所需 在 Visual Studio 中创建一个默认的 MVC 项目,它应该向您展示如何在注册操作中处理注册表单。

Go and download the following project. There is a free pdf document. Work through the entire sample. It makes you understand all the basics you need to know about asp.net mvc framework

Mvc Music Store

And also if you create a default mvc project in visual studio, it should show you how to handle the signup form in the Register action.

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