将模型和额外参数从视图传递到MVC中的操作

时间:2014-08-29 11:42:14

标签: c# asp.net-mvc asp.net-mvc-4 view model

我正在尝试将userid传递给我在控制器中的操作。我的操作就像这样,你可以在这里看到:

[HttpPost]
        [Authorize(Roles = "Admin,Expert")]
        public ActionResult AddExpert(AssistanceJuror assistanceJuror,int UserId)
        {
            User user = objUserRepository.FindBy(i => i.Id == assistanceJuror.UserId).First();
            user.Enable = "فعال";
            assistanceJuror.Date = DateTime.Now;
            objUserRepository.Edit(user);
            objUserRepository.Save();
            objAssistanceJurorRepository.Add(assistanceJuror);
            TempData["Message"] = "کارشناس به معاونت اختصاص داده شد";
            objAssistanceJurorRepository.Save();
            return RedirectToAction("IndexExpert", "Assistance");
        }

所以这个动作需要2个参数,其中一个是我的模型,另一个是userId。所以我需要从视图中获取两个值,我的视图是这样的:

@using ViewModelDomain
@using Repository;
@model DomainClass.AssistanceJuror

@{
    ViewBag.Title = "AddExpert";
}

@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)
    <div class="wrapper-left">
        <div class="wrapper-top-addCompany">
            انتساب کارشناس به معاونت
            <div style="float: left; padding-left: 20px;">
                <div class="divider"></div>
                <div class="buttonPossion">
                    @Html.ActionLink("بازگشت", "Index", "Assistance", new { companyid = ViewBag.firstIdeaId }, new { @class = "buttonBlueLink" })
                </div>
                <div class="divider"></div>
            </div>
        </div>
        <div class="wrapper-Member" style="padding-bottom: 30px; margin-bottom: 5px; width: 100%; min-height: 550px;">
            <h2>اضافه کردن کارشناس جدید
            </h2>
            <br />
            <br />
            <br />
            <div class="editor-label">
                انتخاب معاونت
            </div>
            <div class="editor-field">
                @Html.DropDownListFor(model => model.AssistanceId, (SelectList)ViewBag.listAssistance)
            </div>

            <div class="editor-label">
                انتخاب کارشناس
            </div>
            @*                <div class="editor-field">
                    @Html.DropDownListFor(model => model.userId, (SelectList)ViewBag.listDisableExpert)
                </div>*@
            @{
    if (User.IsInRole("Expert") || User.IsInRole("Admin"))
    {
                <div class="wrapper-Member" style="border-top: 1px solid #d3d3d3; margin-top: 30px; width: 99.5%; border-left: 1px solid #d3d3d3; margin-right: 1px">
                    <div class="tab-list-company" style="margin-right: -1px; width: 180px">
                        <h2 style="font: normal 13px BHoma; margin-right: 10px; margin-top: 2px;">اختصاص کارشناس به معاونت
                        </h2>
                    </div>
                    <div class="list-company" style="border: none; width: 98%">
                        <table>
                            <thead>
                                <tr>
                                    <th>نام و نام خانوادگی</th>
                                    <th>سطح تحصیلات</th>
                                    <th>رشته تحصیلی</th>
                                    <th>شماره همراه</th>
                                    <th>تلفن</th>
                                    <th>ایمیل</th>
                                    @if (User.IsInRole("Expert") || User.IsInRole("Admin"))
                                    {
                                        <th style="width: 20px;">اضافه</th>
                                    }
                                </tr>
                            </thead>
                            @{
                                    userRepository objUserRepository = new userRepository();
                                    //var listJuror = objUserRepository.FindBy(i => i.Permission == "Juror").ToList();
                                List<DomainClass.User> listDisableAssistance = objUserRepository.ReturnUserByEnablePermission("غیرفعال", "Assistance");
                            }

                            @foreach (var temp in listDisableAssistance)
                            {
                                <tr>
                                    <td>
                                        @{
                                string fullName = temp.Name + " " + temp.Family;
                                        }
                                        @Html.DisplayFor(modelItem => fullName)
                                    </td>
                                    <td>
                                        @Html.DisplayFor(modelItem => temp.EducationLevel)
                                    </td>
                                    <td>
                                        @Html.DisplayFor(modelItem => temp.Field)
                                    </td>
                                    <td>
                                        @Html.DisplayFor(modelItem => temp.Mobile)
                                    </td>
                                    <td>
                                        @Html.DisplayFor(modelItem => temp.Tell)
                                    </td>
                                    <td>
                                        @Html.DisplayFor(modelItem => temp.Email)
                                    </td>
                                    <td>


                                            @using (Html.BeginForm("AddExpert", "Assistance", routeValues: new { userid = temp.Id }))
                                       {
                                        <input type="submit" value=""  class="Add" title="اضافه کردن" />
                                       }    
                                    </td>
                                </tr>
                            }

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

}
@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

在这部分

   <td>


                                                @using (Html.BeginForm("AddExpert", "Assistance", routeValues: new { userid = temp.Id }))
                                           {
                                            <input type="submit" value=""  class="Add" title="اضافه کردن" />
                                           }    
                                        </td>

我正在尝试将userId传递给我的操作,但它不起作用,我收到一条错误消息,指出用户ID为空。

祝你好运

2 个答案:

答案 0 :(得分:1)

我认为问题在于您没有将assistanceJuror参数发送到控制器。

new { userid = temp.Id }

应该是

new {assistanceJuror= value, userid = temp.Id }

但我认为AssistanceJuror是一种复杂类型,因此您会收到错误消息。尝试只发送一个id(例如)并从控制器中的数据库获取信息。

像这样的东西

public ActionResult AddExpert(string assistanceJurorId,int UserId)
        {
            var assistanceJuror= //get Your Variable Here;
            User user = objUserRepository.FindBy(i => i.Id == assistanceJuror.UserId).First();
            user.Enable = "فعال";
            assistanceJuror.Date = DateTime.Now;
            objUserRepository.Edit(user);
            objUserRepository.Save();
            objAssistanceJurorRepository.Add(assistanceJuror);
            TempData["Message"] = "کارشناس به معاونت اختصاص داده شد";
            objAssistanceJurorRepository.Save();
            return RedirectToAction("IndexExpert", "Assistance");
        }

并从视图中以这种方式调用

@using (Html.BeginForm("AddExpert", "Assistance", routeValues: new {assistanceJurorId= model.AssistanceId, userid = temp.Id }))

这样您就不会将下拉列表的选定值(存储在model.AssistanceId变量中)传递给控制器​​

assistanceJurorId= model.AssistanceId

但是模型中属性的初始值,因为下拉列表是以这种方式生成的

@Html.DropDownListFor(x => x.AssistanceId, (SelectList)ViewBag.listAssistance))

要在下拉列表中发送所选元素的正确值,我建议您删除Html。 BeginForm并仅在表单提交时向您的控制器发出ajax调用。通过这种方式,您可以确保发送正确的值。

答案 1 :(得分:0)

MVC的最佳方法是在GET方法中创建模型,在View中使用它并在接受Post动词的Action中POST模型。因此,创建一个模型类,在其中添加所有属性(也是用户标识),在顶部的视图添加中

@model YourClassModel

和你的POST方法

public ActionResult AddExpert(YourClassModel model)
相关问题