从客户端发送的值在aspnetMVC中为null



我想将视图中的表单中的数据发送到控制器,但在调试模式下,它说值为null。我确实尝试过使用视图模型,但它发送了null。我的请求包含文件,我想将视频路径保存到模型这就是我尝试过的。是控制器代码

[HttpPost]
public ActionResult Create([Bind(Include = "Id,Title,Title_eng,Title_ar,Description,Description_en,Description_ar,VideoPath")] Video video)
{
var videos = db.Videos.ToList();
var rnd = new Random();
var guid = rnd.Next(999);
var file = HttpContext.Request.Files["file"];
}

这是我的视频模型

public class Video
{  
public int Id { get; set; }
public string Title{ get; set; }
public string Title_en { get; set; }
public string Title_ar { get; set; }
[AllowHtml]
public string Description { get; set; }
[AllowHtml]
public string Description_en { get; set; }
[AllowHtml]
public string Description_ar { get; set; }
public string VideoPath{ get; set; }
[NotMapped]
public HttpPostedFileBase file { get; set; }
}

这是我的观点

@using FinalHospital.Models;
@model FinalHospital.Models.Video
@{
ViewBag.Title = "ثبت خبر جدید";
Layout = "~/Views/Shared/_ManagePanel.cshtml";
} 
@section scripts{
<script src="~/Content/Upload/ImgUpload.js"></script>
<script src="~/Content/Editor/js/tinymce/tinymce.min.js"></script>
<script>
tinymce.init({
selector: '.myTextArea'
});
</script>
}
@section Styles{
<link href="~/Content/Upload/ImgUpload.css" rel="stylesheet" />
}
<section class="content-header">
<ol class="breadcrumb">
<li><a href="/Home/Index"><i class="fa fa-home"></i> خانه</a></li>
<li><a href="@((User.IsInRole("Admin")?"/Admin/Index":User.IsInRole("Operator")?"/Admin/IndexOperator":"/Operators/Index"))"><i class="fa fa-dashboard"></i>پنل مدیریت</a></li>
<li><a href="/Videos/Index">مدیریت خبرها</a></li>
<li class="active">ثبت خبر جدید</li>
</ol>
</section>
<section class="content">
<form action="/Videos/Create/" class="" method="post" enctype="multipart/form-data" role="form" id="">
<p style="position:absolute; left:20px; top:60px;">
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
</p>
<div class="row">
<div class="col-md-8">
<div class="box box-info">
<div class="box-header">
<h4>اطلاعات</h4>
</div>
<div class="box-body">
<div class="row">
<div class="form-group col-md-6">
<label class="control-label col-md-12">عنوان به زبان فارسی</label>
<div class="input-group col-md-12">
<span class="input-group-addon addonStyle-info"><i class="fa fa-envelope"></i></span>
@Html.EditorFor(model => model.Title, new { htmlAttributes = new { @class = "form-control textbox-info" } })
</div>
@Html.ValidationMessageFor(model => model.Title, "", new { @class = "text-danger" })
</div>
<div class="form-group col-md-6">
<label class="control-label col-md-12">عنوان به زبان عربی</label>
<div class="input-group col-md-12">
<span class="input-group-addon addonStyle-info"><i class="fa fa-user-md"></i></span>
@Html.EditorFor(model => model.Title_ar, new { htmlAttributes = new { @class = "form-control textbox-info" } })
</div>
@Html.ValidationMessageFor(model => model.Title_ar, "", new { @class = "text-danger" })
</div>
</div>
<div class="row">
<div class="form-group col-md-6">
<label class="control-label col-md-12">عنوان به زبان انگلیسی</label>
<div class="input-group col-md-12">
<span class="input-group-addon addonStyle-info"><i class="fa fa-user-md"></i></span>
@Html.EditorFor(model => model.Title_en, new { htmlAttributes = new { @class = "form-control textbox-info" } })
</div>
@Html.ValidationMessageFor(model => model.Title_en, "", new { @class = "text-danger" })
</div>
</div>
<div class="row">
<div class="form-group col-md-12">
<label class="control-label col-md-12">توضیح به زبان فارسی</label>
<div class="input-group col-md-12">
@*<span class="input-group-addon addonStyle-info"><i class="fa fa-phone"></i></span>*@
@Html.TextAreaFor(model => model.Description, 5, 0, new { @class = "myTextArea" })
</div>
@Html.ValidationMessageFor(model => model.Description, "", new { @class = "text-danger" })
</div>
</div>
<div class="row">
<div class="form-group col-md-12">
<label class="control-label col-md-12">توضیح به زبان عربی</label>
<div class="input-group col-md-12">
@*<span class="input-group-addon addonStyle-info"><i class="fa fa-phone"></i></span>*@
@Html.TextAreaFor(model => model.Description_ar, 5, 0, new { @class = "myTextArea" })
</div>
@Html.ValidationMessageFor(model => model.Description_ar, "", new { @class = "text-danger" })
</div>
</div>
<div class="row">
<div class="form-group col-md-12">
<label class="control-label col-md-12">توضیح به زبان انگلیسی</label>
<div class="input-group col-md-12">
@*<span class="input-group-addon addonStyle-info"><i class="fa fa-phone"></i></span>*@
@Html.TextAreaFor(model => model.Description_en, 5, 0, new { @class = "myTextArea" })
</div>
@Html.ValidationMessageFor(model => model.Description_en, "", new { @class = "text-danger" })
</div>
</div>
</div>
</div>
</div> <!--اطلاعات تکمیلی-->
@*</div>
<div class="row">*@
<div class="col-md-4">
<div class="box box-info">
<div class="box-header">
<h4>بارگذاری ویديو</h4>
</div>
<div class="box-body">
<div class="form-group col-md-12">
<div class="container image-up">
<div class="col-md-12  ContainerUpload" style="padding-top: 53px;padding-bottom: 53px;">
<div class="row text-center imgDiv" id="Default-uploadBox">
<img src="/Content/add/Images/Icons/outbox.png"><div class="img-Text hidden"></div>
</div>
<div class="uploaderDiv">
<input class="UploadInput" type="file" name="Video" id="files" value="" @*data-val="true" data-val-required="بایستی حداقل یک فایل انتخاب گردد"*@>
<button class="btn" id="UploadBtn">
<i class="fa fa-cloud-upload"> 
</i><span>انتخاب ویدیو</span>
</button>
</div>
@*<div class="removebutton"><i class="fa fa-close"></i></div>*@
</div>
</div>
</div>
</div>
</div>
<div class="col-lg-12" style="padding-top:30px;">
<input type="submit" value="ثبت کلی" class="btn btn-success btn-block" id="loading" />
</div>
</div> <!--آپلود عکس-->
</div>
</form>
</section>

谢谢你的帮助。


我不知道它是否相关,但我在操作中使用了ValidateAntiforgeryTokan注释,然后我可以使用ViewModel获取表单数据并在请求中获取文件。

从您的视图代码中,我可以看到您没有绑定任何名为VideoPath或文件的模型属性,因此模型无法绑定您的属性值。当您声明文件属性时,您需要将此属性绑定到如下所示的视图

@Html.TextBoxFor(model => model.File, new {@class = "form-control", @type = "file"})

然后你会得到一个文件对象到你的模型,声明属性如下

private HttpPostedFileBase _file;
[DataType(DataType.Upload)]
public HttpPostedFileBase File
{
get { return _file; }
set { _file = value; }
}

然后从控制器

[HttpPost]
public ActionResult SaveVideo(Video data)
{
try
{
if (ModelState.IsValid)
{
var uplodedFile = data.File;
//you will get file path from this object property.
//do other work here
}
catch(Exception e){throw;}
} 

如果您有任何疑问,请告诉我们。

最新更新