일부 자식 컨트롤을 렌더링하기위한 부분보기가있는 양식이 있습니다.부분보기에서 전송 된 값 가져 오기
홈페이지보기 :
@model Test_mvc.Models.Entity.Question
@{
ViewBag.Title = "Edit";
Layout = "~/Areas/Admin/Views/Shared/_Layout.cshtml";
}
@using (Html.BeginForm())
{
@*snip*@
<fieldset>
<legend>Profils</legend>
@Html.Action("CheckboxList", "Profil", new { id_question = Model.id })
</fieldset>
<p>
<input type="submit" value="Enregistrer" />
</p>
}
하기 Profil 컨트롤러 (부분 뷰) :
[ChildActionOnly]
public ActionResult CheckboxList(int id_question)
{
var profils = db.Profil.Include("Profil_Question")
.OrderBy(p => p.nom).ToList();
ViewBag.id_question = id_question;
return PartialView(profils);
}
Profil.CheckBoxList보기 :
@model List<Test_mvc.Models.Entity.Profil>
@foreach (var p in Model)
{
<input type="checkbox" name="[email protected](p.id)"
@if (p.Profil_Question.Where(pc => pc.id_question == ViewBag.id_question).Any())
{
@:checked="checked"
} />
@Html.Label("profil_" + p.id, p.nom)
<br />
}
(나는 사용하지 않으 @ Html.CheckBox는 내가 "true, false"가 선택되었을 때 보내지는 것을 좋아하지 않기 때문에). 내가 검사 한 체크 박스를 얻으려면
오늘, 나는이 작업을 수행하지만, 나는 그것이 끔찍한 생각 : (주 뷰)
질문 컨트롤러 :
[HttpPost]
public ActionResult Edit(Question question)
{
if (ModelState.IsValid)
{
db.Question.Attach(question);
db.ObjectStateManager.ChangeObjectState(question, EntityState.Modified);
db.SaveChanges();
// this is what I want to change :
foreach (string r in Request.Form)
{
if (r.StartsWith("profil_") && (Request.Form[r] == "true" || Request.Form[r] == "on")) {
var p_q = new Models.Entity.Profil_Question();
p_q.id_profil = int.Parse(r.Replace("profil_", ""));
p_q.id_question = question.id;
db.AddToProfil_Question(p_q);
}
}
db.SaveChanges();
return RedirectToAction("Index");
}
return View(question);
}
어떻게 것 당신은 마지막 코드 섹션에서 "foreach"를 대체합니까?
감사
좋아요, 조금 나아졌지만 여전히 Request.Form을 사용해야합니다. 그렇죠? 어떻게 ViewModel을 사용하여 부분 뷰의 값을 컨트롤러에 "바인딩"합니까? – thomasb
그 목록을 viewmodel의 속성, 가능한 값 목록 및 선택한 값 목록으로 추가해야합니다. 그런 다음 여기에서 두 번째 대답을 살펴보십시오. http://stackoverflow.com/questions/4872192/ checkboxlist-in-mvc3-0 – glenatron