Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 4.0 在ASP.NET MVC4 Internet应用程序中上载和检索图像和视频url_C# 4.0_Asp.net Mvc 4 - Fatal编程技术网

C# 4.0 在ASP.NET MVC4 Internet应用程序中上载和检索图像和视频url

C# 4.0 在ASP.NET MVC4 Internet应用程序中上载和检索图像和视频url,c#-4.0,asp.net-mvc-4,C# 4.0,Asp.net Mvc 4,我的MVC4互联网应用程序中有以下模型 public class EventModel { public int EventId { get; set;} public string EventTitle { get; set;} public string ImageUrl { get; set;} public string VideoUrl { get;set} } 下面是图像和视频的Create.cshtml代码 @using(Html.BeginFor

我的MVC4互联网应用程序中有以下模型

public class EventModel
{
    public int EventId { get; set;}
    public string EventTitle { get; set;}
    public string ImageUrl { get; set;}
    public string VideoUrl { get;set}
}
下面是图像和视频的Create.cshtml代码

@using(Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)

<div>
@Html.LabelFor(model =>model.ImageUrl)
</div>
<div>
@Html.EditorFor(model =>model.ImageUrl)
@Html.ValidationMessageFor(model =>model.ImageUrl)
</div>

<div>
@Html.LabelFor(model =>model.VideoUrl)
</div>
<div>
@Html.EditorFor(model =>model.VideoUrl)
@Html.ValidationMessageFor(model =>Video.ImageUrl)
</div>

}

如何使创建页面显示按钮从用户计算机上载图像。如何修改控制器操作以在数据库中存储路径,然后如何在index.cshtml页面上检索图像。

要上载文件,请尝试以下操作:

在控制器中:

[HttpPost]
public ActionResult Create(EventModel eventmodel, HttpPostedFileBase file)
{ 
   if (ModelState.IsValid)
   {
      var filename = Path.GetFileName(file.FileName);
      var path = Path.Combine(Server.MapPath("~/Uploads/Photo/"), filename);
      file.SaveAs(path);
      tyre.Url = filename;

      _db.EventModels.AddObject(eventmodel);
      _db.SaveChanges();
      return RedirectToAction("Index");
   }
   return View(eventmodel);
}
和视图:

<div>
   Image
   <input type="file" name="file" id="file" />
   @Html.HiddenFor( model => model.ImageUrl)
   @Html.ValidationMessageFor( model => model.Url )
</div>

形象
@Html.HiddenFor(model=>model.ImageUrl)
@Html.ValidationMessageFor(model=>model.Url)
<div>
   Image
   <input type="file" name="file" id="file" />
   @Html.HiddenFor( model => model.ImageUrl)
   @Html.ValidationMessageFor( model => model.Url )
</div>