3

サーバー側で単純なリストを取得できません。誰かが私を正しい方向に向けてもらえますか?

public class TestList
{
    public string id { get; set; }
    public string name { get; set; }
    public string location { get; set; }
}

形:

@model List<SampleMVC4App.Controllers.TestList>
@{
    ViewBag.Title = "Index";
}
<h2>
    Index</h2>
@using (Html.BeginForm())
{           
    <input name="cust" value="1" type="hidden" />
    <input name="[1].id" value="de107502-284d-459b-80a1-762ce0860cd8" type="hidden" />    
    <input name="[1].name" value="test1" type="hidden" />    
    <input name="[1].location" value="location1" type="hidden" />    
    <a id="AddAnother" href="#">Add</a>
    <input type="submit" value="submit" />
}

コントローラ:

[HttpPost]
public ActionResult Edit(ICollection<TestList> cust) **<---Null**
{
   return View();
}
4

3 に答える 3

2

何時間も働いた後、以下を変更することでなんとか解決しました

<input name="cust" value="1" type="hidden" />

<input name="Index" value="1" type="hidden" />
于 2012-09-19T02:22:54.407 に答える
1

これを試して。あなたのモデルは正しいです。cshtmlページでこれを試してください

 @model List<SampleMVC4App.Controllers.TestList>
    @{
        ViewBag.Title = "Index";
    }
    <h2>
        Index</h2>
    @using (Html.BeginForm())
    {           
        foreach(SampleMVC4App.Controllers.TestList tl in Model)
{
     @model.hiddenfieldfor () // Like this your list will be rendered.
}
    }
于 2012-09-19T05:12:04.400 に答える
0

Edit(FormCollection fm) を使用して、ビューからすべてのフォーム要素を取得してみてください。次に、返された変数を httpPost アクションで処理します。これにより、返される要素を確認する際のデバッグが容易になります。

HttpPOST http://odetocode.com/blogs/scott/archive/2009/04/27/6-tips-for-asp-net-mvc-model-を使用しているときに、Scott Allen によるこの投稿が役に立ちました。バインディング.aspx

于 2012-09-19T01:57:15.313 に答える