【发布时间】:2023-03-12 01:29:02
【问题描述】:
我正在尝试将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 为空。
最好的问候
【问题讨论】:
-
您生成的嵌套表单。不确定是否是问题所在,但它的 html 无效且不受标准支持,因此您应该考虑更改它。如果模型不为空,那么我认为它必须冒泡到外部表单并提交,因此内部表单路由值可能会被忽略。
-
您的呼叫是否以用户 ID 为空的操作到达?
-
@faby 在我第一次单击提交时出现此错误:参数字典包含方法'System.Int32'的不可空类型'System.Int32'的参数'userid'的空条目。 'Presentation.Controllers.AssistanceController' 中的 Web.Mvc.ActionResult AddExpert(DomainClass.AssistanceJuror, Int32)'。可选参数必须是引用类型、可空类型或声明为可选参数。参数名称:参数
-
@EA 尝试将 UserId 的类型从
int更改为int?只是为了检查它是否正常工作.. -
@EA 在您的操作方法的第一行放置一个断点。电话到了吗?
标签: c# asp.net-mvc asp.net-mvc-4 view model