1

だから私は次のコードを持っています:

@model Project.Models.ViewModels.SomeViewModel
@using (Html.BeginForm("SomeAction", "SomeController", new { id = Model.Id}))
        {
            for(int i = 0; i < Model.SomeCollection.Count(); i++)
            {
                @Html.HiddenFor(x => Model.SomeCollection.ElementAt(i).Id)
                <div class="grid_6">
                    @Html.TextAreaFor(x => Model.SomeCollection.ElementAt(i).Text, new { @style = "height:150px", @class = "grid_6 input" })
                </div>
            }
            <div class="grid_6 alpha omega">
                <input type="submit" value="Next" class="grid_6 alpha omega button drop_4 gravity_5" />
            </div>
        }

コントローラー側には次のものがあります。

[HttpPost]
        public ActionResult SomeAction(int id, SomeViewModel model)
        {

            return PartialView("_SomeOtherView", new SomeOtherViewModel(id));
        }

私のビューモデルは次のように設定されています。

public class SomeViewModel
{
        public SomeViewModel()
        {

        }
        public IEnumerable<ItemViewModel> SomeCollection { get; set; }

}

public class ItemViewModel{

      public ItemViewModel(){}

      public int Id {get;set;}

      public string Text{get;set;}

}

SomeActionを実行すると、SomeCollectionは常に空になります。更新された値をユーザーに表示するにはどうすればよいですか。テキストプロパティとIDフィールド。

4

3 に答える 3

2

EditorTemplateを使用する

Views/ YourcontrollerNameの下に EditorTemplate フォルダーを作成し、名前付きのビューを作成します。ItemViewModel.cshtml

ここに画像の説明を入力

そして、そのファイルにこのコードを入れてください

@model  Project.Models.ViewModels.ItemViewModel
<p>
 @Html.EditorFor(x => x.Text) 
 @Html.HiddenFor(x=>x.Id)
</p>

メインビューから、次のように呼び出します

@model  Project.Models.ViewModels.SomeViewModel
@using (Html.BeginForm("SomeAction", "Home", new { id = Model.Id}))
{
    @Html.EditorFor(s=>s.SomeCollection)
    <div class="grid_6 alpha omega">
        <input type="submit" value="Next" class="grid_6 alpha omega button drop_4 gravity_5" />
    </div>
}

これで、HTTPPOSTメソッドに値が入力されます。

値で何をしたいのかわからない(部分ビューを返す?)ので、それについてコメントすることはありません。

于 2012-08-03T15:47:09.083 に答える
0

すべてのコードを投稿したかどうかはわかりません。

アクション メソッドは、新しいモデル オブジェクトを使用して部分的なビューを返すため (何らかの理由で ajax リクエストではなくポスト コールから)、何もしません。

モデルを効果的にアクションに戻してから破棄し、新しいモデル オブジェクトを返します。これが、コレクションが常に空であり、どこにも設定されない理由です。

于 2012-08-03T15:38:25.640 に答える
0

まず、モデルとidモデルのプロパティの両方をコントローラーに送り返すのはなぜでしょうか? それは少し冗長に思えませんか?また、ビューで JavaScriptforループを使用しています。を使用する方がはるかに簡単@foreachです。

とにかく、あなたの問題は、アクションにモデルを受け入れるように指示すると、モデルpostの各プロパティの名前と一致するキーを持つ for 値を検索することです。したがって、次のモデルがあるとしましょう。

public class Employee
{
   public string Name;
   public int ID;
   public string Position;
}

そして、私が次のようにそれを返す場合:

@using(Html.BeginForm("SomeAction", "SomeController"))
{
     <input type="text" name = "name" [...] />    //in your case HtmlHelper is doing this for you, but same thing
     <input type="number" name = "id" [...] />
     <input type="submit" name = "position" [...] />
}

このモデルをコントローラーに戻すには、次のようにする必要があります。

モデルの受け入れ

//MVC matches attribute names to form values
public ActionResult SomethingPosted(Employee emp)
{
    //
}

値のコレクションを受け入れる

//MVC matches parameter names to form values
public ActionResult SomethingPosted(string name, int id, string postion)
{
    //
}

またはこれ:

FormCollection の受け入れ

//same thing as first one, but without a strongly-typed model
public ActionResult SomethingPosted(FormCollection empValues)
{
    //
}

したがって、これがあなたのコードのより良いバージョンです。

あなたの新しい見方

@model Project.Models.ViewModels.SomeViewModel
@{
    using (Html.BeginForm("SomeAction", "SomeController", new { id = Model.Id}))
        {
            foreach(var item in Model)
            {
                @Html.HiddenFor(item.Id)
                <div class="grid_6">
                    @Html.TextAreaFor(item.Text, new { @style = "height:150px", @class = "grid_6 input" })
                </div>
            }
            <div class="grid_6 alpha omega">
                <input type="submit" value="Next" class="grid_6 alpha omega button drop_4 gravity_5" />
            </div>
        }
}

あなたの新しい行動

[HttpPost]
public ActionResult SomeAction(int Id, string Text)
{
    //do stuff with id and text
    return PartialView("_SomeOtherView", new SomeOtherViewModel(id));
}

また

[HttpPost]
public ActionResult SomeAction(IEnumerable<ItemViewModel> SomeCollection)  //can't use someviewmodel, because it doesn't (directly) *have* members called "Id" and "Text"
{
    //do stuff with id and text
    return PartialView("_SomeOtherView", new SomeOtherViewModel(id));
}
于 2012-08-03T15:44:43.453 に答える