简体   繁体   English

ASP.Net MVC自定义数据类型传递其他值

[英]ASP.Net MVC custom datatype passing additional values

I have created the custom "DataType" annotation to use on the model object to tell this is the date field on the view. 我创建了自定义“DataType”注释,用于模型对象,告诉它是视图上的日期字段。 ( [DataType("Date")] ) If I use @Html.EditorFor(model => model.DateCreated) , it will act as a date field and pup up the JavaScript date picker. [DataType("Date")] )如果我使用@Html.EditorFor(model => model.DateCreated) ,它将作为日期字段并播放JavaScript日期选择器。 This is the template I am using under EditorTemplates 这是我在EditorTemplates下使用的模板

@inherits System.Web.Mvc.WebViewPage<DateTime> 
<div class="input-append date" data-date="12-02-2012">
    <input type="text" class="span2">
</div>

View - 查看 -

<div class="control-group">
        @Html.LabelFor(model => model.DateCreated, new { @class = "control-label" })
        <div class="controls" id="date-container">
            @Html.EditorFor(model => model.DateCreated, new { @class="input-append date"})
            @Html.ValidationMessageFor(model => model.DateCreated, null, new { @class = "help-inline" })
        </div>
    </div>

Model - 型号 -

[Display(Name = "Date Created")]
[DataType("Date")]        
public DateTime DateCreated { get; set; }

Controller - 控制器 -

public ActionResult Create(int id)
{
      // Attempt to get new customer object
      GetPaymentResponse paymentResponse = _BillingService.GetPayment(id);

     // Check whether response was successful
     if (paymentResponse.State == BaseResponseState.Valid)
     {              
          paymentResponse.Payment.Type.AvailableOptions = paymentResponse.Payment.Type.AvailableOptions.Where(x => x.Value != "BalancingTransaction").ToList();
          ViewData.Model = paymentResponse.Payment;
          return View();
     }           
}

I need to pass some additional value to my view via datatype from the model. 我需要通过模型中的数据类型将一些额外的值传递给我的视图。

Eg [DataType("Date"), Format("dd/mm/yy"), StartDate("12-02-2012")] 例如[DataType("Date"), Format("dd/mm/yy"), StartDate("12-02-2012")]

Could you please let me know how can I grab these additional value from the template? 您能告诉我如何从模板中获取这些额外的价值吗? (I am new to ASP.Net MVC and I am using MVC 3) (我是ASP.Net MVC的新手,我使用的是MVC 3)

Thanks 谢谢

If you use the Model Binding it should take the value properly I believe. 如果你使用模型绑定,我认为应该正确地取值。

In your view, set the model on the first line, line this: 在您的视图中,在第一行上设置模型,将其排成行:

@model MyViewModel

and then in your controller, instead of passing the Model through the ViewData , do something like this: 然后在你的控制器中,而不是通过ViewData传递Model ,做这样的事情:

var model = new MyViewModel();
// do stuff with your model here

return View(model);

Assuming that StartDate is a property of Payment 假设StartDatePayment的属性

<div class="control-group">
        @Html.LabelFor(model => model.StartDate, new { @class = "control-label" })
        <div class="controls" id="date-container">
            @Html.EditorFor(model => model.StartDate, new { @class="input-append date"})
            @Html.ValidationMessageFor(model => model.StartDate, null, new { @class = "help-inline" })
        </div>
    </div>

If you specify extra information using attributes, the information must be constant for all the instances of the class in which you define the member. 如果使用属性指定额外信息,则对于定义该成员的类的所有实例,该信息必须是常量。 Ie the StartDate will be the same for all instances of your model, becasue the start date specified in the attribute must be a constant. 即,对于模型的所有实例, StartDate将是相同的,因为属性中指定的开始日期必须是常量。

If that serves your purpoes, you can use a custom metadata provider to get specific metadata in your model from your custom attributes. 如果这符合您的要求,您可以使用自定义元数据提供程序从您的自定义属性中获取模型中的特定元数据

If you need to pass different data fro each case, you have to use any of the overloads of EditorFor which allows to pass extra view data. 如果需要为每种情况传递不同的数据,则必须使用EditorFor任何重载,它允许传递额外的视图数据。 Then you can read that extra information from the ViewData in your template. 然后,您可以从模板中的ViewData中读取额外信息。

Be warned that there are some caveats in the metadata providers and custom template implementations, and registration. 请注意,元数据提供程序和自定义模板实现以及注册中存在一些警告。 Take into account if your type can be made nullable, like DateTime? 考虑你的类型是否可以为空,如DateTime?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM