简体   繁体   English

需要MVC3 Razor帮助

[英]MVC3 Razor Help required

I'm hoping you guys can answer me a question? 我希望你们能回答我一个问题?

I've only just started out using ASP.NET MVC3 have come unstuck at a certain point. 我只是刚刚开始使用ASP.NET MVC3,在某些时候还没有解决。 I've been learning this stuff from books and I'm slightly unsure on a few things. 我一直在从书本上学习这些东西,但是我在某些方面还不太确定。

Can a VIEW, only have one @Model reference? 一个VIEW只能有一个@Model引用吗?

At the moment I have a VIEW setup with a HTTP-POST on a ViewResult, that validates the data in the View, entered by the user and then "on post", passes this info to a method that writes it back to a database(ADO.NET - Access). 目前,我在ViewResult上执行了带有HTTP-POST的VIEW设置,该设置用于验证View中的数据,先由用户输入,然后“发布”,然后将此信息传递给将其写回到数据库的方法( ADO.NET-访问)。 Now I need to change my VIEW, so that I can replace a couple of my text boxes for Dropdownlistfor controls. 现在,我需要更改VIEW,以便可以将几个文本框替换为Dropdownlistfor控件。 The Data to populate these controls will need to be passed in from the Database. 填充这些控件的数据将需要从数据库中传递。

Would I be correct in saying that this data needs to be passed in the HTTP-GET Viewresult of the page, and if so, can i reference more than one @Model in this same View (*.cshtml). 我的说法是正确的,该数据需要在页面的HTTP-GET Viewresult中传递,如果是,我可以在同一View(* .cshtml)中引用多个@Model吗?

I have a class that takes in the user response, and this is referenced in the View. 我有一个接受用户响应的类,并且在View中引用了该类。 But will i need to create a new class for the dropdownlist data and reference that too. 但是我是否需要为下拉列表数据创建一个新类并引用它。 So that in the background I populate the data into a SelectListItem, pass it to the View and then populate each drop down control within the view? 这样,在后台我将数据填充到SelectListItem中,将其传递给View,然后填充视图中的每个下拉控件?

I'm sorry if this is poorly written, very hard to explain, I find learning from books hard and I'm pretty stuck now. 很抱歉,如果这本书写得不好,很难解释,我发现很难从书本上学习,现在我很困。 Any help would be appreciated. 任何帮助,将不胜感激。 Just to give me an understanding of how this all wraps around. 只是为了让我了解所有这些情况。 I'm comfortable with the C# syntax, but very unsure of MVC3! 我对C#语法感到满意,但对MVC3不确定!

There are two ways you can handle this. 有两种方法可以处理此问题。

  • Use a View Model. 使用视图模型。

In this scenario you have a class that contains your data model as well as other things required by the view, so something like this: 在这种情况下,您将拥有一个包含数据模型以及视图所需其他内容的类,如下所示:

public class ViewModel
{
    public MyDomainModel Model { get; set; }
    public IEnumerable<SelectListItem> SelectListItems { get; set; }
}
  • Use ViewBag . 使用ViewBag

In this case you add everything extra into the ViewBag dictionary. 在这种情况下,您将所有多余的内容添加到ViewBag词典中。 So in the controller, you'd have stuff like this: 因此,在控制器中,您将拥有以下内容:

ViewBag.SelectListItems = new SelectListItem[] { ... };

Then you can reference in the view itself 然后您可以在视图本身中引用

@Html.DropDownList("myselectlist", ViewBag.SelectListItems)

I think that this will help you pluralsight mvc3 intro . 我认为这将帮助您复数介绍 It sure helped me 它肯定对我有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM