ASP.Net MVC Viewmodel从View转到null控制器

时间:2018-02-15 17:35:01

标签: c# asp.net asp.net-mvc

我想将Viewmodel从我的视图导出到我的控制器,控制器正在接收方法,这是我的观点:

@using Resources
@model MyProject.ViewModel.CertificationViewModel

@{
    ViewBag.Title = Resources.titleCertificationList;

    @section css
        {
        <link href="@Url.Content("~/layout/css/stylesformador.css")" rel="stylesheet" />
        <link href="~/Content/themes/base/jquery-ui.css" rel="stylesheet" />
    }
}

<div class="mainblock">
    <div class="block-group">
        <div class="ttitle title">@HIQResources.titleEdit</div>
    </div>

    @using (Html.BeginForm("Edit", "Certification", FormMethod.Post, new { id = "form1" }))
    {
        @Html.AntiForgeryToken()
        <div class="md-content">
            <div class="form-horizontal">
                @Html.ValidationSummary(true, "", new { @class = "text-danger" })
                @Html.HiddenFor(model => model.Id)

                <div class="form-group">
                    @Html.LabelFor(model => model.pdf, htmlAttributes: new { @class = "control-label col-md-2" })
                    <div class="col-md-8">
                        @*<input type="file" name="file" />*@
                        @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control inputForm", @type="file" } })
                    </div>
                </div>

                <div class="form-group">
                    @Html.LabelFor(model => model.Code, htmlAttributes: new { @class = "control-label col-md-2" })
                    <div class="col-md-8">
                        @Html.EditorFor(model => model.Code, new { htmlAttributes = new { @class = "form-control inputForm", @readonly = "readonly" } })
                        @Html.ValidationMessageFor(model => model.Code, "", new { @class = "text-danger" })
                    </div>
                </div>

                <div class="form-group">
                    @Html.LabelFor(model => model.SelectedEntityId, htmlAttributes: new { @class = "control-label col-md-2" })
                    <div class="col-md-8">
                        @Html.DropDownListFor(model => model.SelectedEntityId, Model.EntitiesDrop, htmlAttributes: new { @class = "form-control inputForm"  })
                        @Html.HiddenFor(model => model.SelectedEntityId)
                        @Html.ValidationMessageFor(model => model.SelectedEntityId, "", new {  @class = "text-danger" })
                    </div>
                </div>

                <div class="form-group">
                    @Html.LabelFor(model => model.StatusId, htmlAttributes: new  { @class = "control-label col-md-2"  })
                    <div class="col-md-8">
                        @Html.DropDownListFor(model => model.StatusId, Model.StatusDrop, htmlAttributes: new { @class = "form-control inputForm" })
                        @Html.HiddenFor(model => model.StatusId)
                        @Html.ValidationMessageFor(model => model.StatusId, "", new { @class = "text-danger" })
                    </div>
                </div>

                <div class="form-group">
                    @Html.LabelFor(model => model.Result, htmlAttributes: new { @class = "control-label col-md-2" })
                    <div class="col-md-8">
                        @Html.EditorFor(model => model.Result, new { htmlAttributes = new { @class = "form-control inputForm" } })
                        @Html.ValidationMessageFor(model => model.Result, "", new { @class = "text-danger"})
                    </div>
                </div>

                <div class="form-group">
                    @Html.LabelFor(model => model.Date, htmlAttributes: new { @class = "control-label col-md-2" })
                    <div class="col-md-8">
                        @Html.EditorFor(model => model.Date, new { htmlAttributes = new { @class = "form-control inputForm" } })
                        @Html.ValidationMessageFor(model => model.Date, "", new { @class = "text-danger" })
                    </div>
                </div>

                <div class="form-group">
                    @Html.LabelFor(model => model.Observation, htmlAttributes: new { @class = "control-label col-md-2" })
                    <div class="col-md-8">
                        @Html.EditorFor(model => model.Observation, new { htmlAttributes = new { @class = "form-control inputForm" } })
                        @Html.ValidationMessageFor(model => model.Observation, "", new { @class = "text-danger" })
                    </div>
                </div>

                <div class="form-group">
                    <div class="col-md-offset-1">
                        <div class="col-md-8">
                            <table class="table table-bordered">
                                <tr>
                                    <th class="col-md-2">
                                        @Html.DisplayNameFor(model => model.FileName)
                                    </th>

                                    <th class="col-md-2">Ficheiro</th>
                                </tr>

                                @foreach (var item in Model.teste)
                                {
                                    <tr>
                                        <td>
                                            @if (item.FileName == null)
                                            {
                                                <h5>Não existem ficheiros para esta certificação.</h5>
                                            }
                                            else
                                            {
                                                @Html.DisplayFor(modelItem => item.FileName)
                                            }
                                        </td>

                                        <td>
                                            @if (item.FileName == null)
                                            {
                                                <h5>Não existem ficheiros para esta certificação.</h5>
                                            }
                                            else
                                            {
                                                <a href="@Url.Action("Download",   "Certification", new { id = Model.Id })">Download</a>
                                                @*<a href="@Url.Action("Edit", "Certification", new { id = Model.Id, teste= Model.click })">Delete</a>*@
                                                <input type="submit" value="Edit" id="Delete" class="btn btn-default btn-sm btn-detail" />
                                            }

                                            @*@Html.ActionLink("Download", "Certification", "Download", new { id = Model.Id })*@

                                        </td>
                                    </tr>
                                }

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

                <div class="form-group">
                    <div class="col-md-offset-2 col-md-8">
                        <input type="submit" value="@HIQResources.buttonSave" class="btn btn-default btn-sm btn-detail" />
                        @Html.ActionLink(HIQResources.buttonBack, "CertificationList", new { id = Model.StudentId, nome = Model.StudentName }, new { @class = "btn btn-default btn-sm btn-edit" })
                    </div>

                </div>
            </div>

        </div>
    }
        </div>

@section Scripts {
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/jqueryval")
    <script src="~/Scripts/HIQTrainingScripts/Certification/Edit.js"></script>
    <script>
        $("#Date").datepicker({ dateFormat: 'yy/mm/dd' });


        $("#Delete").click(function () {
        var vm = $('@Model').serialize();
        alert(vm)
        var token = $('input[name="__RequestVerificationToken"]').val();

        $.ajax({
            url: "/Certification/Edit",
            type: "POST",
            data: {
                __RequestVerificationToken: token,
                vm: vm,
               click: "deu",
            },
            sucess: function (result) {

                alert("sucess");
            }


            });
        });




    </script>

}

这是我控制器中的方法:

[Authorize(Roles = "Admin, Staff")]
[HttpPost]
[ValidateAntiForgeryToken]
    public ActionResult Edit(CertificationViewModel vm, string click) {

                if (click == "deu")
                {
                    int lol = _servicesFactory.GetService<ICertificationManager>().DeleteFile(vm, GetLoggedUser());
                    return View(vm);
                }
    }

我的ViewModel的值总是对我的Jquery变量为空,我不知道为什么,因为它应该带有从视图到控制器的信息,以便我执行我的任务。

我错过了什么吗?

0 个答案:

没有答案