在选择列表asp.net mvc中使用firstname+lastname+email代替id

本文关键字:firstname+lastname+email 代替 id mvc 选择 列表 asp net | 更新日期: 2023-09-27 18:25:56

我有一个包含两个字段的反馈表:project_name和partner的lastName。第一个字段是正确的(project_name),但第二个字段的lastName是我想要的firstName+lastName+电子邮件,如何做到?目前只有姓氏,但我可以为不同的伴侣增加姓氏。以下是我如何创建此表单的代码示例

在我创建反馈控制器的方法中:

  ViewBag.project_id = new SelectList(db.Projects.Where(p => p.project_id == project_id), "project_id", "name");
  ViewBag.Id = new SelectList(db.Users_projects.Where(p => p.project_id == project_id && p.project_role == "partner").Select(up => new { up.AspNetUsers.FirstName,up.AspNetUsers.LastName, up.AspNetUsers.Id }), "Id", "LastName");

以下是我对如何呈现此表单的看法:

<div class="form-horizontal">
    <h4></h4>
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <div class="form-group">
        @Html.LabelFor(model => model.project_id, "Project: ", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownList("project_id", null, htmlAttributes: new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.project_id, "", new { @class = "text-danger" })
        </div>
    </div>
    <div class="form-group">
        @Html.LabelFor(model => model.Id, " For partner : ", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownList("Id", null, htmlAttributes: new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.Id, "", new { @class = "text-danger" })
        </div>
    </div>
    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Create" class="btn btn-default" />
        </div>
    </div>
</div>

在选择列表asp.net mvc中使用firstname+lastname+email代替id

ViewBag.Id =db.Users_projects
   .Where(p => p.project_id == project_id &&p.project_role == "partner")
   .Select(up =>
   new SelectListItem {
                     // You may append User email also here
                     Text= up.AspNetUsers.FirstName +" "+up.AspNetUsers.LastName,
                     Value = up.AspNetUsers.Id.ToString() })
   .ToList();

您可以通过不使用ViewBag之类的动态内容来提高代码的可读性/可维护性,并切换到强类型的视图模型来在操作方法和视图之间传递数据

public class AssignProjectOwnerShipVm
{
  public int SelectedPartner {set;get;}
  public int SelectedProject {set;get;}
  public List<SelectListItem> Projects {set;get;}
  public List<SelectListItem> Partners {set;get;}
}

以及在您的GET操作中

pubilc ActionResult AssignProject()
{
  var vm = new AssignProjectOwnerShipVm();
  vm.Projects = db.Projects.Select(s=>new SelectListItem
                      {
                        Value = s.ProjectId.ToString(),
                        Text = s.ProjectName 
                      }).ToList();
  vm.Partners=db.Users_projects
       .Where(p => p.project_id == project_id &&p.project_role == "partner")
       .Select(up =>
       new SelectListItem {
                     // You may append User email also here
                     Text= up.AspNetUsers.FirstName +" "+up.AspNetUsers.LastName,
                     Value = up.AspNetUsers.Id.ToString() })
   .ToList();
 return View(vm);
}

你的观点

@model AssignProjectOwnerShipVm
@using(Html.BeginForm())
{
  @Html.DrowpDownListFor(s=>s.SelectedProject,Model.Projects,"Select one")
  @Html.DrowpDownListFor(s=>s.SelectedPartner,Model.Partners,"Select one")
<input type="submit" />
}

和你的HttpPost

[HttpPost]
public ActionResult AssignProjecct(AssignProjectOWnerShipVm model)
{
  // check for model.SelectedParter and model.SelectedProject
 // to do  :Save and redirect
}