从视图传递数据到视图Asp.net MVC时,我始终得到一个空条目?

时间:2019-03-01 23:42:14

标签: html asp.net asp.net-mvc

我正在尝试在视图之间传递数据,当我尝试传递数据时,我一直得到null值。

我想要的是从“ contractordetails”视图的操作链接将其在页面上的Contractorid传递到下一个视图“ contractorrequest”,以便可以显示它。

UserId和User Name正常工作,只是不断出现如下所示的错误,指示其为空条目。

enter image description here

我要去哪里错了?

我的代码:

      @Html.ActionLink(
                       linkText: "Contractor Area",
                       actionName: "ContractorRequest",
                       controllerName: "ConDashboard",
                       routeValues: new {area = "Contractor",id = Model.ContractorId},
                                                     htmlAttributes: null )

控制器

    [HttpGet]
    public ActionResult ContractorRequest(ContractorVM model, int contractorid)
    {

        int id = model.ContractorId;

        var addrequest = new AddCalendarRequestVM();
        addrequest.User = new UserVM();

        string username = User.Identity.Name;



        using (Db db = new Db())
        {

            UserDTO dto = db.Users.FirstOrDefault(x => x.Username == username);

            addrequest.User = new UserVM(dto);
        }





        return View("ContractorRequest", addrequest);
    }

查看

   @using (Html.BeginForm("ContractorRequest", "ConDashboard", FormMethod.Post, new { enctype = "multipart/form-data" }))
                        {
                            @Html.AntiForgeryToken()

                        <form class="form-horizontal">



                            <hr />
                            @Html.ValidationSummary(true, "", new { @class = "text-danger" })

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

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

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

1 个答案:

答案 0 :(得分:3)

您尝试做的事情听起来不正确...您想向控制器操作发送get请求,但是操作希望模型可以传入...通常在您需要时要将模型传递给操作方法,可以使用表单并将表单内容发布到操作方法。

因此,如果要将模型发送给action方法,请使用form and post方法(而不是get方法)。


如果您要传递给操作方法的只是一个id,请从该操作方法中删除ContractorVM模型...您得到的错误是因为ContractorVM具有不可为空的id和您没有为此传递任何ID。

因此,此链接:

@Html.ActionLink(
    linkText: "Contractor Area",
    actionName: "ContractorRequest",
    controllerName: "ConDashboard",
    routeValues: new {area = "Contractor",id = Model.ContractorId},
    htmlAttributes: null )

应该采取行动:

[HttpGet]
public ActionResult ContractorRequest(int id) // <-- make sure parameter name matches what you are passing in
{
    // more code
    // ...
}