嗨,我正在尝试让 DropDownList 在我的微不足道的应用程序中工作。我是初学者。我已经花了几天时间查看书籍和网络上的各种说明,所以如果您将我引导到我已经去过那里的一个,那么请谨慎行事。我的情况非常简单和微不足道,但我无法让它发挥作用。我相信帮助我的最好方法是查看我的代码。
我已将我的项目上传到 SkyDrive,我相信您运行我的项目会非常简单。到目前为止,SelectList 呈现在初始的 AddItem 视图中。
这里是项目:
http://cid-5c0bc0a6f7bdc3c6.office.live.com/self.aspx/DebugB/DropDownTwo.zip
该文件夹包含 2 个项目:
- SHAWeb服务
- SHAWeb角色
运行项目:
- 启动 SHAWebService。
- 启动 SHAWebRole,它在端口 8080 上使用 SHAWebService。
- 单击“AddItem”选项卡,您将在 AddItem 视图中看到我的 SelectList 呈现。
我的问题是如何通过“编辑”和“详细信息”视图从那里开始工作?
如果您更喜欢在这里查看我的代码,我将在下面粘贴一些代码:
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();
我在 Web 服务中调用此方法:
/* 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; }
}
最后 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 视图中呈现良好,但是当我单击“添加项目”按钮时,会引发以下异常:
“具有键 'CategoryID' 的 ViewData 项的类型为 'System.Int32' 但必须为 'IEnumerable' 类型。”
我不知道该怎么做才能让它工作。你能帮助我吗?我需要有人帮助我纠正一些细节以使其正常工作。