[英]ASP.NET MVC view models from key / value pairs
How would could I represent a key / value pair as an ASP.NET MVC model? 如何将键/值对表示为ASP.NET MVC模型? My form data is not backed by a strongly typed model.
我的表单数据没有强类型模型的支持。
My first solution was to design my form using Razor and use some extension methods to get the FormElement values. 我的第一个解决方案是使用Razor设计表单并使用一些扩展方法来获取FormElement值。
@model IEnumerable<FormElementKeyValues>
@Html.TextBox(Model.GenerateID("Email"), Model.GetFormElementValue("Email"))<br />
This works but it becomes messy when I want to process data from a POST. 这可以工作,但是当我想处理POST中的数据时会变得混乱。 I have no model so I am forced to fall back to using a FormCollection which means I lose the benefit of strongly typed model and validation.
我没有模型,因此我被迫退回使用FormCollection,这意味着我失去了强类型模型和验证的好处。
A second solution (I haven't tried this yet) would be to create my individual form models and decorate the properties with custom attributes that help me get access to the key / value pairs. 第二种解决方案(我还没有尝试过)是创建我自己的表单模型,并用自定义属性修饰属性,以帮助我访问键/值对。
public SimpleFormModel {
[FormElement("Fullname")]
[Required(ErrorMessage = "Required")]
public string Fullname { get; set; }
[FormElement("Email")]
[Required(ErrorMessage = "Required")]
[DisplayName("E-mail")]
public string Email { get; set; }
}
public ComplexFormModel {
[FormElement("Firstname")]
[Required(ErrorMessage = "Required")]
public string Firstname { get; set; }
[FormElement("Surname")]
[Required(ErrorMessage = "Required")]
public string Surname { get; set; }
[FormElement("Email")]
[Required(ErrorMessage = "Required")]
[DisplayName("E-mail")]
public string Email { get; set; }
}
This would allow me to use a strongly-typed model within my view along with the standard Razor Html Helpers. 这将使我能够在视图中使用标准类型的Razor Html Helpers和强类型模型。
<div class="editor-field">
@(Html.TextBoxFor(model => model.Firstname))
@(Html.ValidationMessageFor(model => model.Firstname))
@(Html.DisplayFor(model => model.Firstname))
</div>
I think you're way over complicating this task... 我认为您已经无法完成此任务了...
Models are there for you to both render, and consume from your Razor views... 模型在那里供您渲染和从Razor视图使用...
So for instance, say I had a website selling tickets to festivals, and I have a order form I want to make reusable across all the different events I'm selling tickets for and that I wanted to pre populate the name of the event in that form... this is how you would do it... 举例来说,假设我有一个出售节日门票的网站,并且我有一个订单表格,希望在要出售门票的所有不同事件中都可以重复使用,并希望在其中预先填写事件的名称表格...这就是你要怎么做...
First of all you need a model, 首先,您需要一个模型,
public class RegistrationViewModel {
[Display(Name = "Event")]
public string EventName { get; set; }
[Required]
[Display(Name = "First name")]
public string FirstName { get; set; }
[Required]
[Display(Name = "Last name")]
public string LastName { get; set; }
}
Next let's imagine we have a Events
Controller with an Action called Register
接下来,让我们想象一下,我们有一个
Events
控制器,其动作名为“ Register
public class Events : Controller
{
public ActionResult Register(int id)
{
Event event = DbSource.FindEventById(id);
RegistrationViewModel model = new RegistrationViewModel
{
EventName = event.Name
}
return this.View(model);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Register(RegistrationViewModel model)
{
if( ! ModelState.IsValid )
{
return this.View(model);
}
// ship the tickets, return thank you view, etc...
}
}
And finally our view.... 最后是我们的看法。
@model RegistrationViewModel
@using (Html.BeginForm("Register", "Events")
{
<div>
@(Html.AntiForgeryToken())
@(Html.LabelFor(model => model.EventName))
@(Html.ValueFor(model => model.EventName))
@(Html.LabelFor(model => model.FirstName))
@(Html.TextBoxFor(model => model.FirstName))
@(Html.ValidationMessageFor(model => model.FirstName))
@(Html.LabelFor(model => model.LastName))
@(Html.TextBoxFor(model => model.LastName))
@(Html.ValidationMessageFor(model => model.LastName))
</div>
}
I've written this on the fly so I don't know if it will compile as is, but what I've shown you is basically all there is to it... 我已经写了这篇文章,所以我不知道它是否可以按原样编译,但是我向您展示的基本上就是它的全部...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.