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

时间:2018-12-12 06:17:55

标签: c# asp.net-mvc razor

我想在视图中将数据从窗体发送到控制器,但是在调试模式下,它说value为空。 我没有尝试使用视图模型,但它发送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获取表单数据并在我的请求中获取文件。

1 个答案:

答案 0 :(得分:1)

从您的查看代码中,我可以看到您没有绑定任何名为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;}
    } 

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