通过的SelectList"&的SelectedValue QUOT;到控制器的操作方法 [英] Pass SelectList "SelectedValue" to Controller Action Method

查看:92
本文介绍了通过的SelectList"&的SelectedValue QUOT;到控制器的操作方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这显示一个用户名(文本),电子邮件(文本)和科(的SelectList)登记表。姓名和电子邮件是pre-填充(我使用的是Windows身份验证,联网的应用程序),我想从下拉列表中的SelectedValue发送到我的控制器作为一个Int32,我不希望将整个的SelectList背部。这份名单是小了,但将增长到相当的规模。

I have a registration form which displays a users Name (textbox), Email (textbox) and Division (SelectList). The Name and Email are pre-populated (I'm using Windows Authentication, Intranet app), and I want to send the SelectedValue from the DropDown to my controller as an Int32, I don't want to send the entire SelectList back. This list is small now, but will grow to considerable size.

我一个名为 RegistrationViewModel 类,它包含了这些领域的公共属性。然而,当我使用的SelectList 为DivisionList,我收到此错误:此对象定义无参数的构造函数

I a class called RegistrationViewModel, it contains public properties for these fields. However, when I use SelectList for the DivisionList, I receive this error: No parameterless constructor defined for this object..

如果我改变类型,它的工作原理没有问题,但司为空或0。有没有办法从下拉列表中的SelectedValue传递给控制器​​的操作方法为的Int32?

If i change the Type, it works no problem, but Division is null or 0. Is there a way to pass the SelectedValue from a DropDown to a Controller Action method as a Int32?

我真的不知道我在做什么,我一直在使用MVC约48小时,观看PDF,微软技术教育大会,并TechDays视频。

I'm not really sure what I'm doing, I've been using MVC for about 48 hours, watched the PDF, TechEd, and TechDays videos.

我的道歉,这里是我的控制器code:

My apologies, here is my controller code:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Register(RegistrationViewModel rvm)
{
    IApplicationContext context = ContextRegistry.GetContext();
    IValidationErrors errors = new ValidationErrors();

    IValidator validator = (IValidator)context.GetObject("RegistrationValidator");
    bool valid = validator.Validate(rvm, errors);

    if (valid)
    	repo.SaveRegistration();
    else
    	ViewData["DivisionList"] = repo.GetDivisions();

    return View(rvm);
}

RegistrationViewModel

public class RegistrationViewModel
{
    public string Name { get; set; }
    public string Email { get; set; }
    //public SelectList DivisionList { get; private set; }
    public int Division { get; set; }    
}

下面是该视图

<%@ Page Language="C#" 
MasterPageFile="~/Views/Shared/Site.Master" 
Inherits="System.Web.Mvc.ViewPage<RegistrationViewModel>" %>

<%@ Import Namespace="Project1.Entities"%>
<%@ Import Namespace="Project1.Models"%>

<asp:Content ID="registerTitle" ContentPlaceHolderID="TitleContent" runat="server">
    Register
</asp:Content>

<asp:Content ID="registerContent" ContentPlaceHolderID="MainContent" runat="server">
...
    <% using (Html.BeginForm())
       { %>
        <div>
            <fieldset>
                <legend>Account Information</legend>
                <p>
                    <label for="name">Name:</label>
                    <%= Html.TextBox("Name", User.Identity.Name.GetDisplayName()) %>
                    <%= Html.ValidationMessage("username") %>
                </p>
                <p>
                    <label for="email">Email:</label>
                    <%= Html.TextBox("email", User.Identity.Name.GetEmailFromLogin()) %>
                    <%= Html.ValidationMessage("email") %>
                </p>              
                <p>
                    <label for="division">Division:</label>
                    <%= Html.DropDownList("DivisionList", ViewData["DivisionList"] as SelectList)%>
                    <%= Html.ValidationMessage("confirmPassword") %>
                </p>
                <p>                
                    <input type="submit" value="Register" />
                </p>
            </fieldset>
        </div>
    <% } %>
</asp:Content>

修改2:

Eilon:这是我改变了它太:

Edit 2:

Eilon: Here is what I changed it too:

控制器:

public ActionResult Register()
{
  ViewData["DivisionList"] = repo.GetDivisions();
  return View();
}

查看:

<%= Html.DropDownList("DivisionValue", ViewData["DivisionList"] as SelectList)%>

我收到此异常:
有型的关键DivisionValue'的IEnumerable'。没有ViewData的项目

I recieve this exception: There is no ViewData item with the key 'DivisionValue' of type 'IEnumerable'.

当我查看更新,以这样的:

When I updated the View to this:

<%= Html.DropDownList("DivisionList", ViewData["DivisionList"] as SelectList)%>

它的工作好了!似乎只有工作,如果所有的科的项目同名。如果我改名字查看崩溃或视图模型项财产被作为0。

It works just great! It only seems to work if all the "Division" items named identically. If I change the name the View crashes or the ViewModel "Division" property is sent as 0.

这是为什么?

推荐答案

RegistrationViewModel 的类型应该包含一个简单类型的属性,如:

The RegistrationViewModel type should contain a simple-typed property such as:

public string DivisionValue { get; set; }

或改变类型为int,日期时间,或任何适当的类型。

Or change the type to int, DateTime, or whatever the appropriate type is.

在HTML和HTTP是被调回的下拉列表中的唯一的事情就是字段的名称,选择的值。

In HTML and HTTP the only thing that gets posted back for a drop down list is the name of the field and the selected value.

要得到的一切投其所好,你也需要改变的观点来呈现不同的输入名称下拉列表:

To get everything to match up you also need to change the view to render a different input name for the drop down list:

<%= Html.DropDownList("DivisionValue", ViewData["DivisionList"] as SelectList)%>

请注意,我使用DivisionValue是的的名单,而 DivisionList 的为所有可用的项目列表。

Notice that I'm using "DivisionValue" is the value of the list, and DivisionList as the list of all available items.

这篇关于通过的SelectList&QUOT;&的SelectedValue QUOT;到控制器的操作方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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