ASP.NET MVC:あなたのViewModelはコレクション/一覧/ IEnumerableをされたときにTextBoxの状態を維持する方法
-
18-09-2019 - |
質問
私は、ASP.NET MVC 2のベータ版を使用しています。私は、リクエスト間でデータを保持する代わりに隠されたフォームフィールドのセッションを使用することを除いて(彼の本のPro ASP.NET MVCフレームワークで)スティーブン・サンダーソンの技術を使用してワークフローのようなウィザードを作成することができます。私は、ページ間を行き来し、私のモデルがコレクションでない場合にすべての問題なしのTextBoxの値を維持することができます。一例では、単純な人のモデルになります:
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
public string Email { get; set; }
}
しかし、私がIEnumerableを周囲を通過するとき、私はこの作業を取得することができません。私の見解では、私はモデルを介して実行し、リスト内の各個人の名前と電子メールのためのTextBoxを生成しようとしています。私は、フォームの罰金を生成することができますし、私は私の値を使用してフォームを送信し、ステップ2に行くことができます。私はステップ2で[戻る]ボタンをクリックすると、しかし、それは空のフォームに戻ってステップ1に私を取ります。私は以前に取り込まフィールドのいずれも存在しません。私が行方不明です何かがあるに違いありません。誰かが私を助けることはできますか?
ここに私のビューがあります:
<% using (Html.BeginForm()) { %>
<% int index = 0;
foreach (var person in Model) { %>
<fieldset>
<%= Html.Hidden("persons.index", index.ToString())%>
<div>Name: <%= Html.TextBox("persons[" + index.ToString() + "].Name")%></div>
<div>Email: <%= Html.TextBox("persons[" + index.ToString() + "].Email")%></div>
</fieldset>
<% index++;
} %>
<p><input type="submit" name="btnNext" value="Next >>" /></p>
<% } %>
そして、ここでは私のコントローラです。
public class PersonListController : Controller
{
public IEnumerable<Person> persons;
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
persons = (Session["persons"]
?? TempData["persons"]
?? new List<Person>()) as List<Person>;
// I've tried this with and without the prefix.
TryUpdateModel(persons, "persons");
}
protected override void OnResultExecuted(ResultExecutedContext filterContext)
{
Session["persons"] = persons;
if (filterContext.Result is RedirectToRouteResult)
TempData["persons"] = persons;
}
public ActionResult Step1(string btnBack, string btnNext)
{
if (btnNext != null)
return RedirectToAction("Step2");
// Setup some fake data
var personsList = new List<Person>
{
new Person { Name = "Jared", Email = "test@email.com", },
new Person { Name = "John", Email = "test2@email.com" }
};
// Populate the model with fake data the first time
// the action method is called only. This is to simulate
// pulling some data in from a DB.
if (persons == null || persons.Count() == 0)
persons = personsList;
return View(persons);
}
// Step2 is just a page that provides a back button to Step1
public ActionResult Step2(string btnBack, string btnNext)
{
if (btnBack != null)
return RedirectToAction("Step1");
return View(persons);
}
}
解決
私の知る限り、これはASP.NET MVC 2のベータ版ではサポートされていない、またそれは、ASP.NET MVC 2 RCでサポートされています。私は、MVCのソースコードを掘って辞書がサポートされているように見えなくIEnumerableをされているモデル<>(またはそのネストされたのIEnumerableオブジェクトを含む)とのIList <>のように、それの相続。
問題がViewDataDictionaryクラスです。特に、GetPropertyValueメソッドは、値のみを引き出すためにPropertyDescriptor.GetValue法を用いて、辞書のプロパティ(GetIndexedPropertyValueを呼び出すことによって)、または単純なプロパティからプロパティ値を取得するための方法を提供します。
この問題を解決するために、私はコレクション(およびネストされたコレクションが含まれていてもモデル)ですモデルを扱うGetCollectionPropertyValueメソッドを作成しました。私は参考のためにここにコードを貼り付けています。注:私は、優雅さについての主張をしない - 実際には、すべての文字列解析は非常に醜いですが、それが動作しているようです。ここに方法はあります:
// Can be used to pull out values from Models with collections and nested collections.
// E.g. Persons[0].Phones[3].AreaCode
private static ViewDataInfo GetCollectionPropertyValue(object indexableObject, string key)
{
Type enumerableType = TypeHelpers.ExtractGenericInterface(indexableObject.GetType(), typeof(IEnumerable<>));
if (enumerableType != null)
{
IList listOfModelElements = (IList)indexableObject;
int firstOpenBracketPosition = key.IndexOf('[');
int firstCloseBracketPosition = key.IndexOf(']');
string firstIndexString = key.Substring(firstOpenBracketPosition + 1, firstCloseBracketPosition - firstOpenBracketPosition - 1);
int firstIndex = 0;
bool canParse = int.TryParse(firstIndexString, out firstIndex);
object element = null;
// if the index was numeric we should be able to grab the element from the list
if (canParse)
element = listOfModelElements[firstIndex];
if (element != null)
{
int firstDotPosition = key.IndexOf('.');
int nextOpenBracketPosition = key.IndexOf('[', firstCloseBracketPosition);
PropertyDescriptor descriptor = TypeDescriptor.GetProperties(element).Find(key.Substring(firstDotPosition + 1), true);
// If the Model has nested collections, we need to keep digging recursively
if (nextOpenBracketPosition >= 0)
{
string nextObjectName = key.Substring(firstDotPosition+1, nextOpenBracketPosition-firstDotPosition-1);
string nextKey = key.Substring(firstDotPosition + 1);
PropertyInfo property = element.GetType().GetProperty(nextObjectName);
object nestedCollection = property.GetValue(element,null);
// Recursively pull out the nested value
return GetCollectionPropertyValue(nestedCollection, nextKey);
}
else
{
return new ViewDataInfo(() => descriptor.GetValue(element))
{
Container = indexableObject,
PropertyDescriptor = descriptor
};
}
}
}
return null;
}
そして、ここで新しいメソッドを呼び出して修正GetPropertyValue方法:
private static ViewDataInfo GetPropertyValue(object container, string propertyName) {
// This method handles one "segment" of a complex property expression
// First, we try to evaluate the property based on its indexer
ViewDataInfo value = GetIndexedPropertyValue(container, propertyName);
if (value != null) {
return value;
}
// If the indexer didn't return anything useful, continue...
// If the container is a ViewDataDictionary then treat its Model property
// as the container instead of the ViewDataDictionary itself.
ViewDataDictionary vdd = container as ViewDataDictionary;
if (vdd != null) {
container = vdd.Model;
}
// Second, we try to evaluate the property based on the assumption
// that it is a collection of some sort (e.g. IList<>, IEnumerable<>)
value = GetCollectionPropertyValue(container, propertyName);
if (value != null)
{
return value;
}
// If the container is null, we're out of options
if (container == null) {
return null;
}
// Third, we try to use PropertyDescriptors and treat the expression as a property name
PropertyDescriptor descriptor = TypeDescriptor.GetProperties(container).Find(propertyName, true);
if (descriptor == null) {
return null;
}
return new ViewDataInfo(() => descriptor.GetValue(container)) {
Container = container,
PropertyDescriptor = descriptor
};
}
繰り返しますが、これは、ASP.NET MVC 2 RCでViewDataDictionary.csファイルです。私はMVCのCodePlexサイト上でこれを追跡するための新しい問題を作成する必要がありますか?