1
我正在構建一個作文競賽頁面。文章可以寫入textarea或者可以上傳。我是新來的asp.net mvc,並跟隨Sanderson的Pro ASP.NET MVC框架。ASP.NET MVC - 文件上傳和模型綁定
我在維護從失敗的字段驗證往返發佈文件時遇到問題。
閱讀this我叫我的輸入文件一樣的模型字段名和我通過這作爲輸入到控制器動作:
[AcceptVerbs(HttpVerbs.Post)]
public ViewResult SubmitEssay(Essay essay, bool acceptsTerms,
HttpPostedFileBase essay_filepath,
HttpPostedFileBase budget_filepath)
{
if (!acceptsTerms)
ModelState.AddModelError("acceptsTerms",
"You must accept the terms and conditions.");
else
{
try
{
// stuff uploaded file values
if (string.IsNullOrEmpty(essay.essay_filepath)
&& (essay_filepath!= null))
{
essay.essay_filepath = essay_filepath.FileName;
}
if (string.IsNullOrEmpty(essay.budget_filepath)
&& (budget_filepath != null))
{
essay.budget_filepath = budget_filepath.FileName;
}
這是工作本身,但是當我去驗證其他字段,往返時文件上傳字段不再包含上傳路徑,這樣當我重新提交時,在填寫必填字段後,就沒有發佈文件。
通過往返維護張貼文件的最佳方式是什麼?你甚至可以設置< input type =「file」/>字段的「值」嗎?我應該使用兩個字段(一個隱藏),並使用jQuery設置文件輸入字段?
其他人做了什麼?
謝謝。
http://www.cs.tut.fi/~jkorpela/forms/file.html#value%29 – Gregoire 2010-01-26 21:33:47
是的,我想我應該只是添加一個評論,說明有錯誤,所以輸入文件將需要再次選擇。 – Scott 2010-01-26 21:38:25
也許添加一些客戶端JS驗證器,以防止提交,除非所有必填字段填寫.. – mare 2010-01-27 00:15:25