Ajax .Beginform Не публикует файл в webapi. - PullRequest
0 голосов
/ 29 марта 2020

Я пытаюсь использовать ajax .beginform для публикации файла и данных формы в проекте веб-API, но смог опубликовать только данные формы. может быть, я что-то делаю не так, пожалуйста, братья, если кто-нибудь сможет мне помочь, буду благодарен PS: извините за мой плохой Engli sh. Вот представление, из которого я пытаюсь опубликовать данные формы.



@model SocialWelfare.Models.Case_ManagementViewModel
<link href="~/Content/css/select2.min.css" rel="stylesheet" />
@using (Ajax.BeginForm("", "", null,
                                    new AjaxOptions
                                    {
                                        HttpMethod = "POST",
                                        Url = ViewBag.ApiUrl + "superadmin/caseManagement/" + (Model.Case_Id == Guid.Empty ? "Create" : "Edit"),
                                        OnSuccess = "UserSuccess",
                                        OnFailure = "Failed"
                                    }, new {enctype = "multipart/form-data", @class = "form-horizontal", role = "form",  @id = "MyForm" }
                                    ))
{
    @Html.HiddenFor(x => x.Case_Id)
    @Html.HiddenFor(x => x.Case_Closed)
    @Html.HiddenFor(x => x.Case_Dt)
    @Html.HiddenFor(x => x.Is_Active)
    @Html.ValidationMessageFor(model => model.Case_Id)
    <div class="row">
        <div class="col-lg-12">
            <div class="panel panel-flat">
                <div class="panel-heading">
                    <div class="col-md-9">
                        <h3>
                            @ViewBag.Title
                        </h3>
                    </div>
                    <div class="heading-elements text-right col-md-3">
                        <a id="btnGoBack" class="pull-left-sm btn btn-default" href="javascript:;" onclick="GoToUrl('SuperAdmin/CaseManagement/Index')">
                            <i class="glyphicon glyphicon-backward"></i>&nbsp;&nbsp;&nbsp;
                            Back to List
                        </a>
                        <ul class="icons-list pull-right">
                            <li><a data-action="collapse"></a></li>
                        </ul>
                    </div>
                    <div class="clearfix"></div>
                </div>
                <div class="panel-body">
                    <div class="form-group">
                        <div class="panel panel-info">
                            <div class="panel-heading">Case Information</div>
                            <div class="panel-body">
                                <div class="form-group">
                                    <div class="col-md-3">
                                        <div class="form-group">
                                            @Html.LabelFor(m => m.Case_Category_Id, new { @class = "control-label" }) <span class="text-danger">*</span>
                                            @Html.DropDownListFor(m => m.Case_Category_Id, new SelectList(new List<string>()), "Select Division", new { @class = "form-control", @data_url = ViewBag.ApiUrl + "common/getcasecategorylist", @data_default = "Select Category", @data_value = Model.Case_Category_Id })
                                            @Html.ValidationMessageFor(model => model.Case_Category_Id)
                                        </div>
                                    </div>
                                    <div class="col-md-3">
                                        <div class="form-group">
                                            @Html.LabelFor(m => m.Case_Sub_Category_Id, new { @class = "control-label" }) <span class="text-danger">*</span>
                                            @Html.DropDownListFor(m => m.Case_Sub_Category_Id, new SelectList(new List<string>()), "Select Parent Sub Category", new { @data_url = ViewBag.ApiUrl + "common/getcasesubcategorylistbasedoncategory", @data_default = "Select Parent Sub Category", @data_value = Model.Case_Sub_Category_Id, @data_cascade_selector = "#Case_Category_Id" })
                                            @Html.ValidationMessageFor(model => model.Case_Sub_Category_Id)
                                        </div>
                                    </div>


                                    <div class="col-md-3">
                                        <div class="form-group">
                                            @Html.LabelFor(m => m.Case_Subject, new { @class = "control-label" }) <span class="text-danger">*</span>
                                            @Html.EditorFor(model => model.Case_Subject, new { htmlAttributes = new { @class = "form-control" } })
                                            @Html.ValidationMessageFor(model => model.Case_Subject)
                                        </div>
                                    </div>
                                    <div class="col-md-3">
                                        <div class="form-group">
                                            @Html.Label("Copy", new { @class = "col-md-2 control-label" })
                                            <div class="col-md-8">
                                                <div class="checkbox">
                                                    @Html.CheckBox("Copy", new { @class = "styled" })
                                                    @Html.ValidationMessageFor(model => model.Is_Regisered_Before)
                                                </div>
                                            </div>
                                        </div>
                                    </div>





                                </div>
                                <hr />
                                <div class="form-group">
                                    <div class="form-group">
                                        @Html.LabelFor(m => m.Is_Regisered_Before, new { @class = "col-md-2 control-label" })
                                        <div class="col-md-8">
                                            <div class="checkbox">
                                                @Html.EditorFor(model => model.Is_Regisered_Before, new { htmlAttributes = new { @class = "styled" } })
                                                @Html.ValidationMessageFor(model => model.Is_Regisered_Before)
                                            </div>
                                        </div>
                                    </div>

                                </div>
                                <div class="form-group" id="MemberSelectionId" hidden>

                                    <div class="form-group">
                                        @Html.LabelFor(m => m.HH_Member_Id, new { @class = "control-label" }) <span class="text-danger">*</span>
                                        @Html.DropDownListFor(m => m.HH_Member_Id, new SelectList(new List<string>()), "Select Member", new { @data_url = ViewBag.ApiUrl + "common/getHHMemberList", @data_default = "Select Member", @data_value = Model.HH_Member_Id })
                                        @Html.ValidationMessageFor(model => model.HH_Member_Id)

                                    </div>

                                </div>
                                <hr />


                            </div>
                        </div>




                    </div>

                    <div class="form-group">
                        <div class="panel panel-info">
                            <div class="panel-heading">Member information</div>
                            <div class="panel-body">
                                <div class="form-group">
                                    <div class="col-md-3">
                                        <div class="form-group">
                                            @Html.LabelFor(m => m.Unique_Id, new { @class = "control-label" }) <span class="text-danger">*</span>
                                            @Html.EditorFor(model => model.Unique_Id, new { htmlAttributes = new { @class = "form-control" } })
                                            @Html.ValidationMessageFor(model => model.Unique_Id)
                                        </div>
                                    </div>
                                    <div class="col-md-3">
                                        <div class="form-group">
                                            @Html.LabelFor(m => m.First_Name, new { @class = "control-label" }) <span class="text-danger">*</span>
                                            @Html.EditorFor(model => model.First_Name, new { htmlAttributes = new { @class = "form-control" } })
                                            @Html.ValidationMessageFor(model => model.First_Name)
                                        </div>
                                    </div>
                                    <div class="col-md-3">
                                        <div class="form-group">
                                            @Html.LabelFor(m => m.Middle_Name, new { @class = "control-label" })
                                            @Html.EditorFor(model => model.Middle_Name, new { htmlAttributes = new { @class = "form-control" } })
                                            @Html.ValidationMessageFor(model => model.Middle_Name)
                                        </div>
                                    </div>
                                    <div class="col-md-3">
                                        <div class="form-group">
                                            @Html.LabelFor(m => m.Last_Name, new { @class = "control-label" }) <span class="text-danger">*</span>
                                            @Html.EditorFor(model => model.Last_Name, new { htmlAttributes = new { @class = "form-control" } })
                                            @Html.ValidationMessageFor(model => model.Last_Name)
                                        </div>
                                    </div>
                                </div>
                                <div class="form-group">

                                    <div class="col-md-3">
                                        <div class="form-group">
                                            @Html.LabelFor(m => m.DOB, new { @class = "control-label" }) <span class="text-danger">*</span>
                                            @Html.TextBoxFor(model => model.DOB, "{0:yyyy-MM-dd}", new { @class = "form-control", @type = "date" })
                                            @Html.ValidationMessageFor(model => model.DOB)
                                        </div>
                                    </div>
                                    <div class="col-md-3">
                                        <div class="form-group">
                                            @Html.LabelFor(m => m.Mobile_Number, new { @class = "control-label" }) <span class="text-danger">*</span>
                                            @Html.EditorFor(model => model.Mobile_Number, new { htmlAttributes = new { @class = "form-control" } })
                                            @Html.ValidationMessageFor(model => model.Mobile_Number)
                                        </div>
                                    </div>
                                    <div class="col-md-3">
                                        <div class="form-group">
                                            @Html.LabelFor(m => m.Originator_Email_Id, new { @class = "control-label" }) <span class="text-danger">*</span>
                                            @Html.EditorFor(model => model.Originator_Email_Id, new { htmlAttributes = new { @class = "form-control" } })
                                            @Html.ValidationMessageFor(model => model.Originator_Email_Id)
                                        </div>
                                    </div>
                                    <div class="col-md-3">
                                        <div class="form-group">
                                            @Html.LabelFor(m => m.Geo_cd_1, new { @class = "control-label" }) <span class="text-danger">*</span>
                                            @Html.DropDownListFor(m => m.Geo_cd_1, new SelectList(new List<string>()), "Select Division", new { @class = "form-control", @data_url = ViewBag.ApiUrl + "common/getlocationlist", @data_default = "Select Division", @data_value = Model.Geo_cd_1 })
                                            @Html.ValidationMessageFor(model => model.Geo_cd_1)
                                        </div>
                                    </div>

                                </div>
                                <div class="form-group">
                                    <div class="col-md-3">
                                        <div class="form-group">
                                            @Html.LabelFor(m => m.Geo_cd_2, new { @class = "control-label" }) <span class="text-danger">*</span>
                                            @Html.DropDownListFor(m => m.Geo_cd_2, new SelectList(new List<string>()), "Select District", new { @data_url = ViewBag.ApiUrl + "common/getotherlocationlist", @data_default = "Select District", @data_value = Model.Geo_cd_2, @data_cascade_selector = "#Geo_cd_1" })
                                            @Html.ValidationMessageFor(model => model.Geo_cd_2)

                                        </div>
                                    </div>

                                    <div class="col-md-3">
                                        <div class="form-group">
                                            @Html.LabelFor(m => m.Geo_cd_3, new { @class = "control-label" }) <span class="text-danger">*</span>
                                            @Html.DropDownListFor(m => m.Geo_cd_3, new SelectList(new List<string>()), "Select Village", new { @data_url = ViewBag.ApiUrl + "common/getotherlocationlist", @data_default = "Select Village", @data_value = Model.Geo_cd_3, @data_cascade_selector = "#Geo_cd_2" })
                                            @Html.ValidationMessageFor(model => model.Geo_cd_3)

                                        </div>
                                    </div>
                                    <div class="col-md-3">
                                        <div class="form-group">
                                            @Html.LabelFor(m => m.Geo_cd_4, new { @class = "control-label" }) <span class="text-danger">*</span>
                                            @Html.DropDownListFor(m => m.Geo_cd_4, new SelectList(new List<string>()), "Select Block", new { @data_url = ViewBag.ApiUrl + "common/getotherlocationlist", @data_default = "Select Block", @data_value = Model.Geo_cd_4, @data_cascade_selector = "#Geo_cd_3" })
                                            @Html.ValidationMessageFor(model => model.Geo_cd_4)

                                        </div>
                                    </div>

                                    <div class="col-md-3">
                                        <div class="form-group">
                                            @Html.LabelFor(m => m.Geo_cd_5, new { @class = "control-label" }) <span class="text-danger">*</span>
                                            @Html.DropDownListFor(m => m.Geo_cd_5, new SelectList(new List<string>()), "Select Constituency", new { @data_url = ViewBag.ApiUrl + "common/getotherlocationlist", @data_default = "Select Constituency", @data_value = Model.Geo_cd_5, @data_cascade_selector = "#Geo_cd_4" })
                                            @Html.ValidationMessageFor(model => model.Geo_cd_5)

                                        </div>
                                    </div>

                                </div>
                                <div class="form-group">
                                    <div class="col-md-3">
                                        <div class="form-group">
                                            @Html.LabelFor(m => m.Location, new { @class = "control-label" }) <span class="text-danger">*</span>
                                            @Html.EditorFor(model => model.Location, new { htmlAttributes = new { @class = "form-control" } })
                                            @Html.ValidationMessageFor(model => model.Location)
                                        </div>
                                    </div>
                                    <div class="col-md-3">
                                        <div class="form-group">
                                            @Html.LabelFor(m => m.Address, new { @class = "control-label" }) <span class="text-danger">*</span>
                                            @Html.EditorFor(model => model.Address, new { htmlAttributes = new { @class = "form-control" } })
                                            @Html.ValidationMessageFor(model => model.Address)
                                        </div>
                                    </div>
                                </div>
                                <div class="form-group">

                                    <div class="form-group">
                                        @Html.LabelFor(m => m.Case_Desc, new { @class = "control-label" }) <span class="text-danger">*</span>
                                        @Html.TextAreaFor(model => model.Case_Desc, new { @class = "form-control", @rows = "10" })
                                        @Html.ValidationMessageFor(model => model.Case_Desc)
                                    </div>

                                </div>
                                <div class="form-group">

                                    <div class="form-group">
                                        @Html.Label("Birthday Certificate", new { @class = "control-label" })
                                        @Html.TextBox("BirthdayCertificate", null, new { @class = "form-control", @type = "file" })

                                    </div>

                                </div>
                                <div class="form-group">

                                    <div class="form-group">
                                        @Html.Label("National Id", new { @class = "control-label" })
                                        @Html.TextBox("NationalId", null, new { @class = "form-control", @type = "file" })

                                    </div>

                                </div>
                            </div>
                        </div>
                    </div>




                    @*<div class="col-md-4">
                            <div class="form-group">
                                @Html.LabelFor(m => m.Is_Active, new { @class = "col-md-4 control-label" })
                                <div class="col-md-8">
                                    <div class="checkbox">
                                        @Html.EditorFor(model => model.Is_Active, new { htmlAttributes = new { @class = "styled" } })
                                        @Html.ValidationMessageFor(model => model.Is_Active)
                                    </div>
                                </div>
                            </div>
                        </div>*@
                </div>
                <div class="panel-footer">
                    <div class="text-center">
                        <button type="submit" class="btn bg-info" >Save <i class="icon-floppy-disk position-right"></i></button>
                        <button class="btn btn-danger" type="button" onclick="GoToUrl('SuperAdmin/CaseManagement/Index')">Cancel<i class="icon-reset position-right"></i></button>
                    </div>
                </div>
            </div>
        </div>
    </div>
}

@section scripts{
    <script type="text/javascript">
        var Url = '@(ViewBag.ApiUrl)common/GetHHMemberBasedOnId';

        $(document).ready(function () {


            $("#HH_Member_Id").change(function () {
                var memberId = $("#HH_Member_Id").val();
                if (memberId) {
                    $.ajax({
                        type: "GET",
                        beforeSend: function (xhr) {
                            xhr.setRequestHeader("Authorization", AuthHeader);
                        },
                        url: Url,


                        data: {
                            id: memberId
                        },
                        success: function (data) {
                            if (data.Status == "OK") {
                                $('#First_Name').val(data.Data.First_name);
                                $('#Middle_Name').val(data.Data.Middle_name);
                                $('#Last_Name').val(data.Data.Last_name);
                                $('#Unique_Id').val(data.Data.Member_unique_id);
                                var date = moment(data.Data.DOB).format('YYYY-MM-DD');
                                console.log(date);
                                $('#DOB').val(date);
                               ;
                                $("#First_Name,#Middle_Name,#Last_Name,#Unique_Id,#DOB").prop("readonly", true);

                            } else {
                                 $('#First_Name').val("");
                                $('#Middle_Name').val("");
                                $('#Last_Name').val("");
                                $('#Unique_Id').val("");
                                 $('#DOB').val("");
                            }

                                }
                        });
                }
            })
             $("#Copy").change(function () {
                var check = document.getElementById("Copy").checked;
                if (document.getElementById("Copy").checked) {

                    var selected = $("#Case_Sub_Category_Id option:selected").text();
                    $("#Case_Subject").val(selected);
                } else {

                    $("#Case_Subject").val("");
                }
            })
            $("#Is_Regisered_Before").change(function () {
                var check = document.getElementById("Is_Regisered_Before").checked;
                if (!document.getElementById("Is_Regisered_Before").checked) {

                    $("#HH_Member_Id").val(0);
                    $("#MemberSelectionId").hide();
                } else {

                    $("#MemberSelectionId").show();
                }
            })

        });

        function UserSuccess(data) {
            if (isSuccess(data)) {
                bootbox.alert({
                    title: "Success",
                    message: data.Messages,
                    callback: function () {
                        window.location = "@Url.Action("Index")";
                    }
                });
            }
        }



    </script>
}






...