将数据从Ilist模型获取到控制器
本文关键字:获取 控制器 模型 Ilist 数据 | 更新日期: 2023-09-27 18:27:46
好的,如果这是新手错误,请原谅。。我一直在寻找各种解决方案,但仍然无法取得任何进展。我需要能够将用户(Id)与公司关联。我已链接到SelectCompInst,并已传入用户Id。我在get方法中建立的公司机构模型上有一个通用的bool标志。该标志在视图中正确显示。如果需要,我可以更改它是复选框的事实。。但我只想在列表中放一个按钮,一个理想情况下会导致回发的链接,以便操作员选择一个要附加到此用户的公司。。我确信有些事情一直都在做。
我看到过各种关于这类问题的引用,说我可能必须使用编辑模板?但我从来没有发现它解释了为什么?肯定不会那么复杂吧?这是我的型号:
[Key]
public int Id { get; set; }
public DateTime RecordCreateDate { get; set; }
[ForeignKey("Address")]
public int AddressId { get; set; }
public string Name { get; set; }
[ForeignKey("PrimaryContact")]
public string PrimaryContactId { get; set; }
public virtual Address Address { get; set; }
public virtual ApplicationUser PrimaryContact { get; set; }
[NotMapped]
public bool UserFlag { get; set; } //Bool to define if user is in this activity type when searching
视图如下:
@model IList<Dune.Models.CompanyInstitution>
@{
ViewBag.Title = "Index";
string staffid = ViewBag.StaffId;
}
<h2>Select Company/Institution for user @ViewBag.UserName</h2>
@using (Html.BeginForm("SelectCompInst","UserAdmin",FormMethod.Post, new {id ="compselect"}))
{
@Html.AntiForgeryToken()
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model[0].Name)
</th>
<th>
@Html.DisplayNameFor(model => model[0].Address)
</th>
<th>
@Html.DisplayNameFor(model => model[0].PrimaryContact)
</th>
<th>
@Html.DisplayNameFor(model => model[0].RecordCreateDate)
</th>
<th></th>
</tr>
@for (int i = 0; i < Model.Count(); i++)
{
<tr>
<td>
@Html.DisplayFor(m => m.ElementAt(i).Name)
</td>
<td>
@{
var AddressString = Model.ElementAt(i).Address.AddressAsString();
}
@Html.DisplayFor(modelItem => AddressString)
</td>
<td>
@{ var NameString = "Not Allocated";
if (Model.ElementAt(i).PrimaryContact != null)
{
NameString = Model.ElementAt(i).PrimaryContact.FullNameNoMidString();
}
}
@Html.DisplayFor(modelItem => NameString)
</td>
<td>
@Html.DisplayFor(modelItem => modelItem.ElementAt(i).RecordCreateDate)
</td>
<td>
@Html.EditorFor(modelItem => modelItem.ElementAt(i).UserFlag)
</td>
<td>
@* Ideally I would like to put a simple 'Select' Link here and also post back the UserId
contained in Viewbag.StaffId.. I figure I can add that to the view model though
if I can get anything at all*@
</td>
</tr>
}
</table>
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Select" />
</div>
}
这是控制器的方法:
//
// GET:
public async Task<ActionResult> SelectCompInst(string StaffId)
{
ApplicationUser user = await UserManager.FindByIdAsync(StaffId);
ViewBag.UserName = "Error User not found!";
if (user != null)
{
ViewBag.UserName = user.FullNameString();
ViewBag.StaffId = StaffId;
}
var companysInstitutions = db.CompanysInstitutions.Include(c => c.Address).Include(c => c.PrimaryContact);
// Walk the list and set the flag if this user is already mapped to this institution.
foreach (CompanyInstitution comp in companysInstitutions)
{
if (user.CompanyInstitutionStaffMember.CompanyInstituteId == comp.Id)
{
comp.UserFlag = true;
}
else
{
comp.UserFlag = false;
}
}
IList<CompanyInstitution> companys = await companysInstitutions.ToListAsync();
return View(companys);
}
//
// POST:
[HttpPost, ActionName ("SelectCompInst")]
[ValidateAntiForgeryToken]
public async Task<ActionResult> SelectComp(IList<CompanyInstitution> Company )
{
//
// Code removed because it doesnt get this far with any data :)
//
ModelState.AddModelError("", "Something failed.");
return View();
}
我用小提琴看了看后面的帖子,但一无所获。。尽管Post功能正在受到攻击。IList公司为空。。。我确信这归结为我的一个根本误解,但我已经为此挣扎了两天。。我不得不听从更明智的社区的摆布:)
我想针对表中的每一行放置一个链接,并简单地传递回该行的id和用户id。谢谢。
Stephens评论后进行进一步编辑。。该场景正在编辑用户。用户可以将CompanyInstituion Id作为其模型的一部分。我希望向运营商(不一定是用户)提供一份公司列表,允许他们将公司id附加到该用户。因此,在概念上,它非常简单——列出一份可供选择的公司名单。按下公司线路上的"选择"链接,然后返回控制器进行排序。因此,我需要在Viewbag中保留UserId——是的,如果需要,我可以创建一个视图模型,但仅获得公司id将是一个开始)。。就这样……出示列表,选择一个并返回……无论如何,我真的不想使用复选框。。我尝试了一些东西之后,有点"成功了"。在添加复选框之前,我最初在循环中有提交按钮。我确实试着把公司Id放在按钮上,但也没用。
如果操作员必须选择一家将与用户关联的公司,那么采取所选公司id和用户id的POST操作将更有意义
[HttpPost, ActionName ("SelectCompInst")]
[ValidateAntiForgeryToken]
public async Task<ActionResult> SelectComp(int companyId, string staffId)
{
...
}
然后在您的视图中,您可以在表的每一行上有多个表单和一个提交按钮:
@model IList<Dune.Models.CompanyInstitution>
@{
ViewBag.Title = "Index";
string staffid = ViewBag.StaffId;
}
<h2>Select Company/Institution for user @ViewBag.UserName</h2>
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model[0].Name)
</th>
<th>
@Html.DisplayNameFor(model => model[0].Address)
</th>
<th>
@Html.DisplayNameFor(model => model[0].PrimaryContact)
</th>
<th>
@Html.DisplayNameFor(model => model[0].RecordCreateDate)
</th>
<th></th>
</tr>
</thead>
<tbody>
@for (var i = 0; i < Model.Count; i++)
{
<tr>
<td>
@Html.DisplayFor(m => m[i].Name)
</td>
<td>
@{
var AddressString = Model[i].Address.AddressAsString();
}
@Html.DisplayFor(modelItem => AddressString)
</td>
<td>
@{
var NameString = "Not Allocated";
if (Model[i].PrimaryContact != null)
{
NameString = Model[i].PrimaryContact.FullNameNoMidString();
}
}
@Html.DisplayFor(modelItem => NameString)
</td>
<td>
@Html.DisplayFor(modelItem => modelItem[i].RecordCreateDate)
</td>
<td>
@Html.EditorFor(modelItem => modelItem[i].UserFlag)
</td>
<td>
@using (Html.BeginForm("SelectCompInst", "UserAdmin", new { companyId = Model[i].Id, staffId = staffid }, FormMethod.Post, new { @class = "compselect" }))
{
@Html.AntiForgeryToken()
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Select" />
</div>
}
</td>
</tr>
}
</tbody>
</table>
基本上,我们会在表的每一行都有一个HTML表单,它会将必要的信息传递给服务器:
@using (Html.BeginForm(
actionName: "SelectCompInst",
controllerName: "UserAdmin",
routeValues: new { companyId = Model[i].Id, staffId = staffid },
method: FormMethod.Post,
htmlAttributes: new { @class = "compselect" }))
{
@Html.AntiForgeryToken()
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Select" />
</div>
}