好吧,看起來有人應該遇到這種情況,但我還沒有找到任何有用的東西。這是我的場景。如何構建多對多關係的創建視圖
我有一個客戶和技能模型。其目標是讓客戶對每一套技能進行評分。
所以,在這裏,我將粘貼模型,視圖模型,控制器和視圖。在最後的關鍵問題
模型
namespace ClientSkills.Models
{
public class Client
{
public int ClientId { get; set; }
public string Name { get; set; }
public ICollection<ClientSkills> Skills { get; set; }
}
public class Skill
{
public int SkillId { get; set; }
public string Name { get; set; }
public ICollection<ClientSkills> Clients { get; set; }
}
public class Rating
{
public int RatingId { get; set; }
public string Name { get; set; }
public ICollection<ClientSkills> ClientSkills { get; set; }
}
public class ClientSkills
{
[Key, Column(Order=0)]
public int ClientId { get; set; }
[Key, Column(Order = 1)]
public int SkillId { get; set; }
public int RatingId { get; set; }
public Rating Rating { get; set; }
public Skill Skill { get; set; }
public Client Client { get; set; }
}
public partial class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public DbSet<Client> Client { get; set; }
public DbSet<Skill> Skill { get; set; }
public DbSet<Rating> Rating { get; set; }
public DbSet<ClientSkills> ClientSkills { get; set; }
}
}
視圖模型
namespace ClientSkills.Models
{
public class RateViewModel
{
public RateViewModel()
{
this.Skills = new List<SkillViewModel>();
}
public RateViewModel(Client client)
{
this.ClientId = client.ClientId;
this.Skills = new List<SkillViewModel>();
if (client.Skills.Count == 0)
{
var context = new ApplicationDbContext();
foreach (var skill in context.Skill)
{
var skillVM = new SkillViewModel(skill);
skillVM.SelectedRatingid = context.Rating.First(r => r.Name == "No aplica").RatingId;
Skills.Add(skillVM);
}
}
else
{
foreach (var item in client.Skills)
{
var skillVM = new SkillViewModel(item);
skillVM.SelectedRatingid = item.SkillId;
this.Skills.Add(skillVM);
}
}
}
public int ClientId { get; set; }
public List<SkillViewModel> Skills { get; set; }
}
public class SkillViewModel
{
public SkillViewModel()
{
Ratings = new List<Rating>();
}
public SkillViewModel(Skill skill)
{
var context = new ApplicationDbContext();
this.Ratings = context.Rating.ToList();
this.SkillId = skill.SkillId;
this.SkillName = skill.Name;
}
public SkillViewModel(ClientSkills item)
{
var context = new ApplicationDbContext();
this.Ratings = context.Rating.ToList();
this.SkillId = item.SkillId;
this.SkillName = item.Skill.Name;
this.SelectedRatingid = item.RatingId;
}
public List<Rating> Ratings { get; set; }
public int SelectedRatingid { get; set; }
public int SkillId { get; set; }
public string SkillName { get; set; }
}
}
控制器
namespace ClientSkills.Controllers
{
public class RateController : Controller
{
//
// GET: /Rate/Create
public ActionResult Create()
{
//first, we assume there is an already selected user
var context = new ApplicationDbContext();
var client = context
.Client
.Include(c => c.Skills)
.First(c => c.Name.ToLower() == "ricker");
var model = new RateViewModel(client);
return View(model);
}
}
}
VIEW
此視圖是由創建與嚮導創建視圖,刪除它創建的無用場,並把下面
<table>
<tr>
<td>Skill</td>
<td>Rate</td>
</tr>
@foreach (var item in Model.Skills)
{
<tr>
<td>
@Html.DisplayFor(model => model.Skills.First(i => i.SkillName == item.SkillName).SkillName, new { @class = "control-label col-md-2" })
</td>
<td>
@Html.DropDownListFor(x => item.SelectedRatingid, new SelectList(item.Ratings, "RatingId", "Name"), "Seleccione el valor")
</td>
</tr>
}
</table>
問題的代碼是在該行skillVM.SelectedRatingid = context.Rating.First(r => r.Name == "No aplica").RatingId;
我的RateViewModel(Client client)
構造爲selectedRatingId屬性設置默認值。我需要確保所有客戶都具備所有技能,即使他們忘記或繞過其中一個。
加載視圖時,我確信ratesid已設置好,技能名稱看起來不錯,但drop dpwn列表不顯示選定的默認值。
如何使視圖將selectedRatingId屬性綁定到下拉列表控件?
我不是.net mvc專業版,很抱歉,如果這是一個愚蠢的評論。你可以試試像''''SelectList'''構造函數的另一個重載:''@ Html.DropDownListFor(x => item.SelectedRatingid,new SelectList(item.Ratings,「RatingId」,「Name」,item.SelectedRatingid ),「Seleccione el valor」)'''通知第四個參數爲''''SelectList''' – Varinder
當然,我會嘗試和發佈 –
它工作!請psot它作爲答案給你的觀點。有什麼不同?這是我第一次使用那個超載 –