如何在视图中加载多个控件

发布于 2024-11-13 06:28:57 字数 562 浏览 1 评论 0原文

我仍然不知道如何开始在 MVC 3 上准备这样的表单。

我是初学者,到目前为止我所学到的只是将数据从控制器绑定到强类型视图。但据我所知,我们只能从返回语句

public ActionResult(int id) 返回一个变量 { // 做一些逻辑 返回视图(角色); 现在上面

的代码将角色列表返回到视图。但是我将如何传递其他详细信息,例如许可证状态、组织..等*

另一个复杂的示例:

假设我的表单需要显示国家/地区[下拉]、州[下拉]、部门[组合框列表]、组织[等详细信息单选按钮列表],所有员工列表[表/网格]

如何使用单个 RETURN 显示所有控件值?

注意:* 我假设所有详细信息(例如角色、许可证状态、组织等)都是从数据库中获取的。

我希望我的解释很清楚,如果我需要进一步解释,请告诉我。

另外,我对这个愚蠢的问题感到抱歉,这是因为我正处于 MVC 的第一个学习阶段

在此处输入图像描述

I am still clueless how to start preparing a form like this on MVC 3.

I am a beginner and all I have learned till now is to bind data from controller to the strongly typed view. But as I know that we can return only one varibale from the return statment

public ActionResult(int id)
{
// Do some logic
return View(role);
}

Now the above code return the role list to the view. But how would I pass other details also like Licence state, organization.. etc *

Another complex example:

Let say my form need to display details like Country [drop down], State [Drop down], Department [ComboBox list], Organization [radio button list], List of all employee [table/Grid]

How would I display all the controls value with single RETURN?

Note: * I assume that all the detials like role, Licence state, Organization etc I am fetching from database.

I hope I am clear with my explanation, please let me know if I need to explain it bit further.

Also, I am sorry for this stupid question, this is because I am in my very first learning stage of MVC

enter image description here

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

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

发布评论

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

评论(1

吾家有女初长成 2024-11-20 06:28:57

您将编写一个视图模型:

public class MyViewModel
{
    public SomeModel1 Section1 { get; set; }
    public SomeModel2 Section2 { get; set; }
    public SomeModel3 Section3 { get; set; }
}

并在控制器操作中将此视图模型返回到视图:

public class HomeController: Controller
{
    public ActionResult Index()
    {
        var model = new MyViewModel
        {
            Section1 = ...,
            Section2 = ...,
            Section3 = ...,
        };
        return View(model);
    }

    [HttpPost]
    public ActionResult Index(MyViewModel model)
    {
        ... process the model when the form is submitted
    }
}

并在视图中:

@model MyViewModel
@using (Html.BeginForm())
{
    @Html.EditorFor(x => x.Section1)
    @Html.EditorFor(x => x.Section2)
    @Html.EditorFor(x => x.Section3)
    ...
}

当然,每个部分都会有强类型编辑器模板:

  • ~/Views/Shared/EditorTemplates/SomeModel1 .cshtml
  • ~/Views/Shared/EditorTemplates/SomeModel2.cshtml
  • ~/Views/Shared/EditorTemplates/SomeModel3.cshtml
  • ...

这将代表每个部分的部分内容

You would write a view model:

public class MyViewModel
{
    public SomeModel1 Section1 { get; set; }
    public SomeModel2 Section2 { get; set; }
    public SomeModel3 Section3 { get; set; }
}

and in your controller action you will return this view model to the view:

public class HomeController: Controller
{
    public ActionResult Index()
    {
        var model = new MyViewModel
        {
            Section1 = ...,
            Section2 = ...,
            Section3 = ...,
        };
        return View(model);
    }

    [HttpPost]
    public ActionResult Index(MyViewModel model)
    {
        ... process the model when the form is submitted
    }
}

and in the view:

@model MyViewModel
@using (Html.BeginForm())
{
    @Html.EditorFor(x => x.Section1)
    @Html.EditorFor(x => x.Section2)
    @Html.EditorFor(x => x.Section3)
    ...
}

and of course you will have strongly typed editor templates for each section:

  • ~/Views/Shared/EditorTemplates/SomeModel1.cshtml
  • ~/Views/Shared/EditorTemplates/SomeModel2.cshtml
  • ~/Views/Shared/EditorTemplates/SomeModel3.cshtml
  • ...

which will represent the partial contents of each section

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