泛舟湖上清波郎朗
以下代碼可以正常工作:@using (Html.BeginForm("Upload", "Upload", FormMethod.Post,?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? new { enctype = "multipart/form-data" })){? ? @Html.ValidationSummary(true)? ?
? ? ? ? Select a file ? ? ? ? ? ? }并按預期生成:? ??? ? ? ? ? ? Select a file ? ? ? ? ? ? 在另一方面,如果你正在寫其他的服務器端結構的上下文中的代碼,如if或foreach您應該刪除@之前using。例如:@if (SomeCondition){? ? using (Html.BeginForm("Upload", "Upload", FormMethod.Post,?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? new { enctype = "multipart/form-data" }))? ? {? ? ? ? @Html.ValidationSummary(true)? ? ? ? ? ? ? ? ? ? Select a file ? ? ? ? ? ? ? ? ? ? ? ? }}就您的服務器端代碼而言,這是繼續的方法:[HttpPost]public ActionResult Upload(HttpPostedFileBase file)?{? ? if (file != null && file.ContentLength > 0)?? ? {? ? ? ? var fileName = Path.GetFileName(file.FileName);? ? ? ? var path = Path.Combine(Server.MapPath("~/content/pics"), fileName);? ? ? ? file.SaveAs(path);? ? }? ? return RedirectToAction("Upload");}