私はtable
を持っています。各tr
は、input
の要素name
を各行に固有の値に設定することによってグループ化されています。 POSTFormCollectionのキーグループに安全にアクセスするにはどうすればよいですか?
[HttpPost]
public ActionResult Update(FormCollection collection)
{
try
{
//doin work on collection...order assumed static
return RedirectToAction("Index");
}
catch
{
return View();
}
}
で
例えば、
<td>
<input data-field="opps" type="text" value="somevalue" name="@item.Code" />
</td>
<td>
<input data-field="asc" type="text" value="somevalue2" name="@item.Code" />
</td>
私System.Web.MVC.FormCollectionは私が<td>
を定義するのと同じ順序でグループ化されています。私は命令を引き受けるのは好きではないが、data-field
にアクセスできない場合、何か他に何ができるのか分からない(おそらく、データフィールド値を接頭辞としてname
に追加して、それをすべてカスタムコレクションとRegex ..しかし、それはナツィーと思われる)。
data-field
にアクセスする方法はありますか?このようにして、ビュー内で新しい列を並べ替えたり追加したりするのは壊れません。あなたが作成する場合
[HttpGet]
public ViewResult Create()
{
MyModel sampleModel = new MyModel();
return View(sampleModel);
}
[HttpPost]
public ActionResult Create(MyModel sampleModel)
{
if (ModelState.IsValid)
{
TempData["Error"] = "There were errors. Please correct the problem and submit again";
return View(sampleModel);
}
// At this point everything is fine and you can access data in your sampleModel
if (sampleModel.Age >= 16)
{
return RedirectToAction("LegalAccess");
}
else
{
TempData["Error"] = "You must be 16 or over to access this site";
return RedirectToAction("AgeRestriction");
}
}
:
データを収集するためにモデルを使用しようとしましたか?そうすれば、FormCollection内の項目の順序について多くのことを心配する必要はありません。 –
@Huske - いいえ、私はあなたが何を意味するのか分かりません。リンクの例がありますか、または回答を投稿したいですか? –
そのデータフィールドのサーバー側にアクセスすることはできません。私はここであなたの実際の目標に少しあいまいです。もっと説明できますか? – Mallioch