【发布时间】:2018-06-13 16:03:03
【问题描述】:
我的视图是这样的
我的网址链接http://localhost:63897/UploadImages?id=1361。 1361 是我的pr_id。我需要将 1361 的 id 从 url 传递到数据库,但它会为空。
这是我的控制器代码:
public ActionResult UploadImages(int id) {
ViewBag.prid = id;
return View();
}
[HttpPost]
public ActionResult UploadImages([Bind(Include = "id,photo_url,photo_caption,photo_credit,pr_id")] Photo photos, HttpPostedFileBase photo_file)
{
if (ModelState.IsValid)
{
if (photo_file != null && photo_file.FileName != null && photo_file.FileName != "")
{
try
{
string path = Path.Combine(Server.MapPath("~/Images/Releases"), Path.GetFileName(photo_file.FileName));
photo_file.SaveAs(path);
string f1 = path.Substring(path.LastIndexOf("\\"));
string[] split = f1.Split('\\');
string newpath = split[1];
string imagepath = "~/Images/Releases/" + newpath;
photos.photo_url = imagepath;
_db.Photos.Add(photos);
_db.SaveChanges();
}
catch (Exception ex)
{
ViewBag.Message = "ERROR:" + ex.Message.ToString();
}
return RedirectToAction("List");
}
}
return View();
}
查看:
@Html.HiddenFor(model => model.pr_id, new { @Value = ViewBag.id })
【问题讨论】:
标签: c# asp.net asp.net-mvc asp.net-mvc-5