데이터베이스에서 벗어난 편차 항목 목록이 있습니다. 목록에 심각도가이며 null 일 수 있습니다. 그 이유는 내가 SeverityNotNull 속성을 null 값을 -1로 변환했기 때문입니다. BTW 심각도는 0-3이 될 수 있습니다.DropDownList에서 선택한 항목 설정
표시하려고합니다. 편도 각 심각도는 줄 단위로 DropdownList 여야하는 항목입니다. 물론 DropdownList에서 적절한 항목을 선택해야합니다.
내 뷰 모델은 다음과 같습니다
[MetadataType(typeof(DeviationMetaData))]
public partial class Deviation {
public int SeverityNotNull {
get { return Severity == null ? -1 : Severity.Value; }
}
...
}
public class DeviationMetaData {
public Nullable<int> Severity { get; set; }
...
}
public class DeviationViewModel {
public Deviation Dev { set; get; }
public IEnumerable<Deviation> Deviations {
get {
DeviationRepository dm = new DeviationRepository();
return dm.GetAll;
}
}
public DeviationViewModel() {
WindowsIdentity current = WindowsIdentity.GetCurrent();
string name = current.Name.Split(new[] { '\\' })[1];
Dev = new Deviation { CreatedBy = name, CreatedDate = DateTime.Now };
}
}
내 컨트롤러는 다음과 같습니다
@model DeviationViewModel
@using (Html.BeginForm()) {
<table>
<tr>
<th>
@Html.DisplayNameFor(model => model.Dev.Severity)
</th>
</tr>
@foreach (var item in Model.Deviations) {
<tr>
<td>
@Html.DropDownListFor(modelItem => item.SeverityNotNull, (SelectList)ViewBag.SelectSeverity)
</td>
</tr>
}
</table>
</fieldset>
}
</div>
내가 SeverityNotNull 값을 확인하고 그들은 올바른 :
public ActionResult Index() {
IList<SelectListItem> items = new List<SelectListItem> {
new SelectListItem{Text = "", Value = "-1"},
new SelectListItem{Text = "Minor", Value = "1"},
new SelectListItem{Text = "Major", Value = "2"},
new SelectListItem{Text = "Critical", Value = "3"}
};
ViewBag.SelectSeverity = new SelectList(items, "Value", "Text");
return View(new DeviationViewModel());
}
내보기입니다. 결과에는 드롭 다운리스트가 있지만 아무것도 선택되지 않습니다. 이것이 문제입니다. 몇 가지 아이디어를 제공해 주시겠습니까? 감사.
'Severity'가 null 일 수있는 경우,'SeverityNotNull'의 요점은 무엇입니까? 왜 널 값을 -1로 변환해야합니까? 이 모든 잘못에 대한 당신의가는 그리고 심지어는 어쨌든 –
-1은 Dropdownlist에서 빈 텍스트의 색인을하려고합니다 다시 게시에 바인딩되지 않습니다. 분명히 null은 인덱스가 될 수 없습니다. 왜 틀렸어? 어쨌든 당신의 아이디어는 무엇입니까? – Franziee
그렇게하지 않아도됩니다. 다른 여러 문제가 있으므로 곧 답변을 게시하겠습니다. 뷰 모델에는 데이터베이스 액세스 코드가 없어야합니다. –