2
MVC 2アプリケーションで連絡先フォームを取得しました。MVC2モデルのプロパティからDisplayName属性を取得
「ContactModel
」のすべてのプロパティをプログラムで電子メールで送信したいと考えています。
[DisplayName("First Name")]
public string FirstName {get; set ;}
:
ContactModel
はこのよう
DisplayName
属性を設定します...それが重要な場合は
[HttpPost]
public ActionResult Contact(ContactModel model)
if(!ModelState.IsValid){
TempData["errorMessage"] = "You are a failure!";
return View(model);
}
else{
var htmlMessage = new StringBuilder("<table>");
const string templateRow = "<tr><td>{0}: </td><td><strong>{1}</strong></td></tr>";
/* ************************************************* */
/* This is the part I need some serious help with... */
/* ************************************************* */
foreach(var property in someEnumerableObjectBasedOnMyModel){
htmlMessage.AppendFormat(templateRow,
// the display name for the property
property.GetDisplayName(),
// the value the user input for the property (HTMLEncoded)
model[property.Key]
);
}
/* ************************************************* */
htmlMessage.Append("</table>");
// send the message...
SomeMagicalEmailer.Send("[email protected]", "[email protected]", "Subject of Message ", htmlMessage.ToString());
TempData["message"] = "You are awesome!";
return RedirectToAction("Contact", "Home");
}
}
:ここ
は、私は擬似っぽいコードでやってみたいものです
私はDisplayNameの名前を繰り返さないことでこれをうまく保ちたいと思います。
具体的には、ContactModelの各プロパティを列挙し、そのDisplayNameを取得し、送信された値を取得したいと思います。
+1これは素晴らしいです。反射なしでそれを行う方法はありますか?アプリはMediumTrustで実行されます。 : -/ –
@David - 属性を直接使用しません。 – tvanfosson
passersの場合: 'ModelMetadata.FromLambdaExpression(式、新しいViewDataDictionary (モデル)).DisplayName;' –