ASP.Net MVC框架和数据绑定 [英] ASP.Net MVC framework and databinding

查看:124
本文介绍了ASP.Net MVC框架和数据绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些麻烦抓MVC框架背后的一些概念。我做了一个非常简单的应用程序,产品分类

制作画面就干脆用一个下拉列表中显示的类别列表中,该产品的名称,并提交。

在一个正常的.net应用程序,我将数据绑定在Page_Load服务器DropDownList的,但在一个MVC应用程序,什么是从数据库中检索我的类别,并将其添加到下拉列表中选择最好的方法是什么?

(对不起,我的问题是非常noobish但不幸的是资源是备用的MVC和例子常因早期变化破裂)

解决方案

我不知道我是否完全掌握,但如果你的页面显示的是一个单一的产品,唯一的用户输入的只是从下拉选择一个类别下拉列表中,我可以八九不离十帮助(但我是一个小白呢!)。

引用此页面:

http://weblogs.asp.net/scottgu/archive/2008/05/27/asp-net-mvc-$p$pview-3-release.aspx

您会希望创造你的控制器的选择列表进行分类(可能是引用一个ID,并显示名称)。然后,这个的SelectList添加到您的ViewData。你可以交替,从您的视图设置此为您ViewData.Model的组成部分和参考。

在你看来,你使用的HtmlHelper为DropDownList的是需要的选择列表作为参数。<​​/ P>

以上的这种联系应该传达它要好得多,这只是一个简单的总结。这种联系是preVIEW 3,但我认为它应该仍然适用。

I am having some trouble grasping some concepts behind the MVC framework. I am doing a very simple application which categorizes products.

The Creation screen will simply use a dropdown list showing the list of categories, the name of the product and submit.

On a normal .Net app, I would databind a server dropdownlist in the Page_Load, but in a MVC app, what is the best way to retrieve my categories from the database and add them to the dropdown list?

(Sorry, my question is extremely noobish but unfortunately resources are spare on MVC, and examples are often broken due to early changes)

解决方案

I'm not sure if I totally grasp, but if your page is displaying a single product and the only user input is just to select a category from a drop down list, I can sorta help (but I'm a noob too!).

Referencing this page:

http://weblogs.asp.net/scottgu/archive/2008/05/27/asp-net-mvc-preview-3-release.aspx

You are going to want to create a SelectList in your controller for Categories (probably referencing an ID, and displaying a Name). You then add this SelectList to your ViewData. You could alternatively set this as part of your ViewData.Model and reference that from your View.

In your view you use the HtmlHelper for DropDownList that takes a SelectList as a parameter.

That link above should convey it much better, this is just a quick summary. That link is for preview 3, but I think it should all still apply.

这篇关于ASP.Net MVC框架和数据绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆