您好我正在尝试让DropDownList在我的应用程序中运行。我是初学者。我已经花了几天时间查看书籍和网络上的各种说明,所以如果你指引我到一个我已经去过那里的话,我会很谨慎。我的情况非常简单而且微不足道,但我无法让它发挥作用。我相信帮助我的最好方法是查看我的代码。
我已将我的项目上传到SkyDrive,我相信您可以很轻松地运行我的项目。到目前为止,SelectList在初始的AddItem视图中呈现。
以下是项目:
http://cid-5c0bc0a6f7bdc3c6.office.live.com/self.aspx/DebugB/DropDownTwo.zip
该文件夹包含2个项目:
运行项目:
我的问题是如何通过编辑和详细信息视图从那里开始工作?
如果您希望在此处查看我的代码,我将在下面粘贴一些代码:
SalesItemController看起来像这样:
public ActionResult AddItem()
{
using (var WS = new SHAServiceReference.SHAServiceClient())
{
var categories = WS.GetCategories().ToList();
IEnumerable<SelectListItem> lstCategory = categories.Select(cat => new SelectListItem
{
Value = Convert.ToString(cat.CategoryID),
Text = cat.CategoryName
});
var salesItemModel = new SalesItemModel();
// Retrieving the UserID of the user which is going to add a sales item.
salesItemModel.SellerUserID = 24; // Hardcoded only for this example //(int)Session["userId"];
salesItemModel.CategorySelectList = lstCategory;
return View(salesItemModel);
}
}
[HttpPost]
public ActionResult AddItem(SalesItemModel salesItemModel, HttpPostedFileBase file)
{
if (ModelState.IsValid)
{
// This variable will capture the SalesItemID of the newly created SalesItem.
var salesItemId = 0;
//Attempt to add the SalesItem
using (var WS = new SHAServiceReference.SHAServiceClient())
{
salesItemId = WS.AddSalesItem
(
salesItemModel.SellerUserID,
salesItemModel.CategoryID,
salesItemModel.Title,
salesItemModel.Description,
salesItemModel.Price,
false
);
return View(salesItemModel);
}
}
return View(salesItemModel);
}
AddItem视图如下所示:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<SHAWebRole.Models.SalesItemModel>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Add Item
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>
Add Item To Sell</h2>
<% using (Html.BeginForm("AddItem", "SalesItem", FormMethod.Post, new { enctype = "multipart/form-data" }))
{%>
<%: Html.ValidationSummary(true)%>
<fieldset>
<legend>Fill in info about what you are selling</legend>
<div id="userDiv">
<%: Html.HiddenFor(model => model.SalesItemID)%>
<%: Html.HiddenFor(model => model.SellerUserID)%>
Sales Item Category
<div>
<%: Html.DropDownListFor(m => m.CategoryID, Model.CategorySelectList, "Select Category") %>
</div>
当我使用此方法从Web服务检索SalesItemController中的CategoryID和CategoryName时var categories = WS.GetCategories()。ToList();
我在网络服务中调用此方法:
/* Returns a list with the Categories for SalesItems */
public List<CategoryDto> GetCategoriesBL()
{
var categories = (from eachCategory in context.Categories
select new CategoryDto
{
CategoryID = eachCategory.CategoryID,
CategoryName = eachCategory.CategoryName
}
).ToList();
return categories;
}
我在上面的方法(CategoryDto)中使用的数据传输对象如下所示:
[DataContract]
public class CategoryDto
{
[DataMember]
public int CategoryID { get; set; }
[DataMember]
public string CategoryName { get; set; }
}
Finaly SalesItemModel如下所示:
public class SalesItemModel
{
public int SalesItemID { get; set; }
public int SellerUserID { get; set; }
public int CategoryID { get; set; }
public IEnumerable<SelectListItem> CategorySelectList { get; set; }
目前DropDownList在初始AddItem视图中渲染得很好,但是当我单击“Add Item”按钮时,会抛出以下异常:
“具有键'CategoryID'的ViewData项的类型为'System.Int32',但必须是'IEnumerable'类型。”
我不知道如何让它发挥作用。你能帮助我吗? 我需要有人帮我纠正一些细节才能使其发挥作用。
答案 0 :(得分:2)
好的,在这里,修改您的视图模型以包含所选值的属性。如果可能的话,避免使用ViewData也是一种好习惯,所以我们也可以添加SelectListItem ...
public class SalesItemModel
{
...
public IEnumerable<SelectListItem> CategorySelectList { get; set; }
public int CategoryID { get; set; }
...
}
现在,在控制器中更改填充的选择列表......
var categories = WS.GetCategories().ToList();
IEnumerable<SelectListItem> lstCategory = categories.Select(x => new SelectListItem
{
Value = Convert.ToString(cat.CategoryID),
Text = cat.CategoryName
});
var salesItemModel = new SalesItemModel();
salesItemModel.CategorySelectList = lstCategory;
在视图中,现在您已经强烈键入了selectlistitems ...
<% Html.DropDownListFor(m => m.CategoryID, Model.CategorySelectList, "Select Category") %>
最后修改帖子操作以使用已发布的选择...
salesItemId = WS.AddSalesItem
(
salesItemModel.SellerUserID,
salesItemModel.CategoryID, //change here
salesItemModel.Title,
salesItemModel.Description,
salesItemModel.Price,
false
);