mohammad khalifeh
mohammad khalifeh

Reputation: 15

value sent from client is null in aspnet MVC

i want to send data from form in view to controller but in debugging mode it say value is null. i did try using view model but it sends null. my request contain file and i want to save video path to model it's what i tried. it's controller code

 [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"];
    }

and it's my Video Model

  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; }

}

and here is my view

@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>

thank you for your help .


i don't know is it related or not but i used ValidateAntiforgeryTokan annotation in my action and then i could get form data with ViewModel and get file in my request.

Upvotes: 0

Views: 87

Answers (1)

Taskin Khaleque
Taskin Khaleque

Reputation: 161

From your view code i can see that you do not bind any model property named VideoPath or file so model can't bind your property value. As you declare file property so you need to bind this property to view like below

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

Then you will get a file object to your model, Declare property like below

private HttpPostedFileBase _file;

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

Then from controller

[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;}
    } 

If you have any query let us know.

Upvotes: 1

Related Questions