I have feedback form with two fields : project_name and lastName of partner. The first fiels is correct(project_name) but instead lastName of second field I want there firstName+lastName+email, how to do it? At the moment there is only lastname, but I can have multiply lastnames for different partners. Below is sample of code how I create this form.
In my create method of feedbackController:
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");
Here is my view how render this form:
<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>
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();
You can make your code more readable/maintainable by not using dynamic stuff like ViewBag and switch to a strongly typed viewmodel to pass the data between your action method and view
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;}
}
and in your GET action
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);
}
And your view
@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" />
}
And your HttpPost
[HttpPost]
public ActionResult AssignProjecct(AssignProjectOWnerShipVm model)
{
// check for model.SelectedParter and model.SelectedProject
// to do :Save and redirect
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.