2

こんにちは私は私の些細なアプリで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」である必要があります。」

それを機能させるために何をすべきかわかりません。手伝って頂けますか?これを機能させるには、詳細を修正するのを手伝ってくれる人が必要です。

4

1 に答える 1

2

では、ビューモデルを変更して、選択した値のプロパティを含めます。可能であればViewDataの使用を避けることもお勧めします。そうすれば、SelectListItemsも追加できます...

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
    );
于 2011-05-06T16:02:28.050 に答える