こんにちは私は私の些細なアプリでDropDownListを機能させようとしています。私は初心者です。私はすでに本やウェブでさまざまな指示を見て数日を過ごしたので、あなたが私に指示してくれれば、私はすでにそこにいます。私の状況は本当に単純で些細なことですが、それを機能させることはできません。私を助ける最善の方法は、私のコードを調べることだと思います。
プロジェクトをSkyDriveにアップロードしました。プロジェクトを実行するのは、非常に簡単だと思います。これまでのところ、SelectListは最初のPASSWORDビューでレンダリングされます。
プロジェクトは次のとおりです。
http://cid-5c0bc0a6f7bdc3c6.office.live.com/self.aspx/DebugB/DropDownTwo.zip
フォルダには2つのプロジェクトが含まれています。
- SHAWebService
- SHAWebRole
プロジェクトを実行するには:
- SHAWebServiceを起動します。
- SHAWebRoleを起動すると、ポート8080でSHAWebServiceが消費されます。
- [segment]タブをクリックすると、SelectListのレンダリングがPASSWORDビューに表示されます。
私の質問は、編集ビューと詳細ビューを介してそこからどのように機能させるかです。
ここで私のコードを確認したい場合は、その一部を以下に貼り付けます。
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);
}
PASSWORDビューは次のようになります。
<%@ 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 category = 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は最初のPASSWORDビューで正常にレンダリングされますが、[アイテムの追加]ボタンをクリックすると、次の例外がスローされます。
「キー「CategoryID」を持つViewDataアイテムのタイプは「System.Int32」ですが、タイプは「IEnumerable」である必要があります。」
それを機能させるために何をすべきかわかりません。手伝って頂けますか?これを機能させるには、詳細を修正するのを手伝ってくれる人が必要です。