![](/img/trans.png)
[英]ASP.NET MVC 2 - How can I get the selected value from a drop down into my view model?
[英]How to determine selected value for drop down lists in asp.net MVC View?
asp.net MVC的新功能。 我有一個包含多個字段的表。 我想建立輸入規則,用戶必須在其中選擇Field1或Field2的下拉列表中的值。 如果同時為Field1和Field2選擇了一個值,或者Field1或Field2都沒有選擇的值,則用戶將無法發布。
我的兩難選擇是決定應該在哪里記錄輸入邏輯或規則(Controller vs View)? 據我了解,最佳實踐是保持Controller簡單而“瘦”,那么我是否可以使用Razor在View中放置輸入規則? 如果是這樣,怎么辦?
預先感謝您的建議。
這是模型:
public class Order
{
public int OrderID {get; set;}
public string Field1 {get; set;}
public string Field2 {get; set;}
//other properties
}
我在Controller中使用viewbag創建下拉列表:
// GET: Order/Create
public ActionResult Create()
{
ViewBag.Field1 = new SelectList(db.Field1.OrderBy(x => x.Name), "Name", "Name");
ViewBag.Field2 = new SelectList(db.Field2.OrderBy(z => z.Clip), "Clip", "Clip");
return View();
}
視圖:
<div class="form-group">
@Html.LabelFor(model => model.Field1, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.Field1, (SelectList)ViewBag.Field1, "Select one...", htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Field1, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Field2, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.Field2, (SelectList)ViewBag.Field2, "Select one...", htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Field2, "", new { @class = "text-danger" })
</div>
</div>
您可以執行以下操作。 我在代碼中解釋了將每個項目放在哪里:
視圖:
@*put model reference here*@
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>IndexValid9</title>
</head>
<body>
@using (Html.BeginForm())
{
<table>
<tr>
<td>
@Html.DropDownListFor(m => m.SelectedA,
new SelectList(Model.TableAList, "Value", "Text"))
@Html.ValidationMessageFor(model => model.SelectedA)
</td>
</tr>
<tr>
<td>
@Html.DropDownListFor(m => m.SelectedB,
new SelectList(Model.TableAList, "Value", "Text"))
@Html.ValidationMessageFor(model => model.SelectedB)
</td>
</tr>
</table>
<input type="submit" value="submit" />
}
</body>
</html>
控制器型號
//You can put this in a model folder
public class FieldViewModel
{
public FieldViewModel()
{
TableAList = Utilites.GetTableAList();
TableBList = Utilites.GetTableBList();
}
public List<SelectListItem> TableAList { get; set; }
public List<SelectListItem> TableBList { get; set; }
public int SelectedA { get; set; }
public int SelectedB { get; set; }
}
//You can put this in its own folder
public class Utilites
{
public static List<SelectListItem> GetTableAList()
{
List<SelectListItem> list = new List<SelectListItem>();
try
{
using (BreazEntities31 entity = new BreazEntities31())
{
entity.TableAs.
OrderBy(r => r.Text).ToList().ForEach(r => list.Add(
new SelectListItem { Text = r.Text, Value = r.Value.ToString() }));
}
}
catch (Exception e)
{ }
//add <select> to first item
list.Insert(0, new SelectListItem { Text = "", Value = "" });
return list;
}
public static List<SelectListItem> GetTableBList()
{
List<SelectListItem> list = new List<SelectListItem>();
try
{
using (BreazEntities31 entity = new BreazEntities31())
{
entity.TableBs.
OrderBy(r => r.Text).ToList().ForEach(r => list.Add(
new SelectListItem { Text = r.Text, Value = r.Value.ToString() }));
}
}
catch (Exception e)
{ }
//add <select> to first item
list.Insert(0, new SelectListItem { Text = "", Value = "" });
return list;
}
}
public class HomeController : Controller
{
[HttpPost]
public ActionResult IndexValid9(FieldViewModel fieldViewModel)
{
//put breakpoint here to see selected values from both ddl, interrogate fieldViewModel
return View();
}
public ActionResult IndexValid9()
{
FieldViewModel fieldViewModel = new FieldViewModel();
return View(fieldViewModel);
}
表:
CREATE TABLE [dbo].[TableA](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Text] [varchar](20) NULL,
[Value] [int] NULL,
CONSTRAINT [PK_TableA] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object: Table [dbo].[TableB] Script Date: 6/27/2017 3:59:07 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[TableB](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Text] [varchar](20) NULL,
[Value] [int] NULL,
CONSTRAINT [PK_TableB] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
SET IDENTITY_INSERT [dbo].[TableA] ON
GO
INSERT [dbo].[TableA] ([Id], [Text], [Value]) VALUES (1, N'tableaTxt', 1)
GO
INSERT [dbo].[TableA] ([Id], [Text], [Value]) VALUES (2, N'tableaTxt2', 2)
GO
SET IDENTITY_INSERT [dbo].[TableA] OFF
GO
SET IDENTITY_INSERT [dbo].[TableB] ON
GO
INSERT [dbo].[TableB] ([Id], [Text], [Value]) VALUES (1, N'tablebTxt', 1)
GO
INSERT [dbo].[TableB] ([Id], [Text], [Value]) VALUES (2, N'tablebTxt2', 2)
GO
SET IDENTITY_INSERT [dbo].[TableB] OFF
GO
驗證邏輯通常是在View Model類中使用數據注釋實現的。 但是,對於您的情況,您可以使用以下自定義驗證屬性
public class OnlyOneValueAttribute : ValidationAttribute
{
public string TheOtherPropertyName { get; private set; }
public OnlyOneValueAttribute(string theOtherPropertyName)
{
TheOtherPropertyName = theOtherPropertyName;
}
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
var theProperty = validationContext.ObjectType.GetProperty(validationContext.MemberName);
var theValue = theProperty.GetValue(validationContext.ObjectInstance, null) as string;
var theOtherProperty = validationContext.ObjectType.GetProperty(TheOtherPropertyName);
var theOtherValue = theOtherProperty.GetValue(validationContext.ObjectInstance, null) as string;
if (string.IsNullOrEmpty(theValue) && !string.IsNullOrEmpty(theOtherValue) || !string.IsNullOrEmpty(theValue) && string.IsNullOrEmpty(theOtherValue))
{
return ValidationResult.Success;
}
return new ValidationResult(ErrorMessage);
}
}
然后可以在模型中使用它
public class Order
{
public int OrderID { get; set; }
[OnlyOneValue(nameof(Field2))]
public string Field1 { get; set; }
[OnlyOneValue(nameof(Field1))]
public string Field2 { get; set; }
//other properties
}
然后在控制器中測試模型狀態有效性
[HttpPost]
public ActionResult Test(Order model)
{
if (ModelState.IsValid)
{
//OK logic
}
return View();
}
但是,為了方便UX,建議您回顧此用例的實現。
希望這可以幫助。
如果驗證規則是業務邏輯/需求的一部分,則應在后端/服務器端實現。 請記住,用戶可以使用郵遞員等工具將數據發布到您的路線,從而繞過您的UI / View驗證。 顯示/隱藏輸入的Javascript / UI驗證是您可以采取的額外步驟,以使用戶更輕松。 如果此驗證僅在應用程序的某個位置進行,我將使其簡單,愚蠢(KISS)並僅在控制器內部進行驗證:
[HttpPost]
public ActionResult Create(OrderViewModel order)
{
var bothFieldsAreEmpty = string.IsNullOrEmpty(order.Field1) && string.IsNullOrEmpty(order.Field2);
var bothFieldsAreFilled = !string.IsNullOrEmpty(order.Field1) && !string.IsNullOrEmpty(order.Field2);
if (bothFieldsAreEmpty || bothFieldsAreFilled)
{
ModelState.AddModelError("", "Please select either Field1 or Field2, not both.");
}
if (ModelState.IsValid)
{
// Do something
}
return View(order);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.