[英]Dropdown in dynamic views mvc4
我們正在使用動態視圖來顯示和編輯記錄。
假設我有一個模型A:
public class A
{
public int AID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int organizationid { get; set; }
}
和B類:
public class B
{
public int organizationid { get; set; }
public string orgnmae { get; set; }
public string orgdesc { get; set; }
}
表B的organizationid是表A中的外鍵。
現在我有了動態編輯器視圖,
@model dynamic
@using (Html.BeginForm("Edit", null, FormMethod.Post))
{
@Html.EditorForModel()
<input type="submit" value="Edit" />
}
如果我們選擇編輯任何A的記錄,它將顯示帶有所有文本框的此編輯器視圖。 但是對於organizationid,我不應該在其中顯示整數值。 相反,我應該顯示表B的可用組織的下拉列表,用戶可以選擇其中一個進行編輯。
如果我按照您的建議解決此問題,則可以對A進行更改。如果您不打算使用viewmodel,則需要擴展它的屬性。
public class A
{
[ScaffoldColumn(false)]
public int OrganizationId { get; set; }
[Display(Name="OrganizationId")]
[UIHint("DropDownList")]
public IEnumerable<SelectListItem> Organizations { get; set; }
}
您會注意到,我已經覆蓋了組織的名稱字段,以便將值作為OrganizationId反饋到模型綁定中。 建立此模型時,將要創建orgranizations屬性,作為相關組織實例中selectlistitems的列表或數組。
我還將原始的organizationid設置為不被腳手架打住,希望它能阻止它參與渲染過程。
您當然需要在編輯器模板中創建模板DropDownList。
HTH
我不太確定我是否正確理解您的問題。 我從不使用EditorFor
,我只是直接從視圖模型添加字段。 它給了我更多的控制權。 據我所知,您似乎想編輯class A
並在下拉列表中添加class B
?
給定您的屬性名稱,這就是我將如何定義class A
和class B
class A
,如果我記錯了,請原諒。 class A is class User
class B is class Organization
。 您需要對屬性進行更好的描述,以便人們可以更好地閱讀它們。
public class User
{
public int UserId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int OrganizationId { get; set; }
}
public class Organization
{
public int OrganizationId { get; set; }
public string OrganizationName { get; set; }
public string OrganizationDescription { get; set; }
}
您需要一個視圖模型來表示視圖中的數據。
public class EditUserViewModel
{
public int UserId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int OrganizationId { get; set; }
public IEnumerable<Organization> Organizations { get; set; }
}
public ActionResult Edit(int id)
{
// Get the user by id
User user = userRepository.GetById(id);
// You can use a mapping tool here to map from domain model to view model.
// I did it differently for the sake of simplicity
EditAViewModel viewModel = new EditAViewModel
{
UserId = user.UserId,
FirstName = user.FirstName,
LastName = user.LastName,
OrganizationId = user.OrganizationId,
Organizations = organizationRepository.GetAll()
};
// Return the populated view model to the view
return View(viewModel);
}
[HttpPost]
public ActionResult Edit(EditAViewModel viewModel)
{
if (!ModelState.IsValid)
{
viewModel.Organizations = organizationRepository.GetAll();
return View(viewModel);
}
// If validation succeeds do what ever you have to do here, like edit in database
}
這就是您的視圖外觀。
@model YourProject.ViewModels.Users.EditUserViewModel
@using (Html.BeginForm())
{
<table>
<tr>
<td class="edit-label">First Name:</td>
<td>
@Html.TextBoxFor(x => x.FirstName)
@Html.ValidationMessageFor(x => x.FirstName)
</td>
</tr>
<tr>
<td class="edit-label">Last Name:</td>
<td>
@Html.TextBoxFor(x => x.LastName)
@Html.ValidationMessageFor(x => x.LastName)
</td>
</tr>
<tr>
<td class="edit-label">Organization:</td>
<td>
@Html.DropDownListFor(
x => x.OrganizationId,
new SelectList(Model.Organizations, "OrganizationId", "OrganizationName", Model.OrganizationId),
"-- Select --"
)
@Html.ValidationMessageFor(x => x.OrganizationId)
</td>
</tr>
</table>
<button id="SaveButton" type="submit">Save</button>
}
這就是我的方法。 您可以修改代碼以適合您的方案。
我希望這有幫助。
嗨嘗試這樣
模型
public class CustomerModel
{
public int CustomerId { get; set; }
public string customerName { get; set; }
}
視圖
@model dynamic
@{
Layout = null;
}
@Html.DropDownList("CustomerId", (SelectList) ViewBag.CustomerNameID,"--Select--")
Contrller
[HttpGet]
public ActionResult CustomerInfo()
{
var List = GetCustomerName();
ViewBag.CustomerNameID = new SelectList(List, "CustomerId", "customerName");
return View();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.