2
我想在將其傳遞給控制器的操作之前更改表單值。但它拋出Collection is read-only.
如何在ASP.NET MVC中發佈後更改表單值?
public class PersonController : Controller
{
public ActionResult Add()
{
return View();
}
[HttpPost]
[PersianDateConvertor("birthday")]
public ActionResult Add(FormCollection collection)
{
string firstName = collection["firstName"];
string lastName = collection["lastName"];
string birthday = collection["birthday"];
return View();
}
}
public class PersianDateConvertorAttribute : ActionFilterAttribute
{
string[] fields;
public PersianDateConvertorAttribute(params string[] persianDateFieldNames)
{
if (persianDateFieldNames == null)
fields = new string[] { };
else
fields = persianDateFieldNames;
}
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
foreach (var field in fields)
{
string value = filterContext.HttpContext.Request.Form[field];
filterContext.HttpContext.Request.Form.Remove(field); //throws Collection is read-only
filterContext.HttpContext.Request.Form.Add(field, ConvertToGregorian(value));
// or filterContext.HttpContext.Request.Form[field] = ConvertToGregorian(value);
}
base.OnActionExecuting(filterContext);
}
}
由於某些原因,我不想轉換d ata我的行動 – Jalal
看看這些SO問題:http://stackoverflow.com/questions/323265/c-can-i-modify-request-forms-variables和http://stackoverflow.com/questions/5561207 /請求形式的信息收集是隻-只讀-時,試圖對設置文本框內容 – Baz1nga