我目前正在开发我的第一个 MVC3 应用程序(使用 Razor 视图引擎),并决定使用开源 Telerik Q1 2012 控件,因为它们将提供我需要的许多功能(并且看起来也不错)。现在我遇到的问题是使用 Telerik Editor 控件并绑定到我的视图模型。我在页面上有标准的 Html.EditorFor() 控件,可以正确返回 ViewModel 中的值,但绑定到 Telerik Editor 的属性为空。他们的文档完全没用(它只提到了一次EditorFor),而且他们似乎也没有在论坛上回答太多问题。我的主要问题是,如何将 Telerik MVC3 编辑器绑定到模型并让它设置绑定到它的属性?我的视图模型代码如下(感谢您提供的任何帮助,
public class SupportViewModel
{
[Display(Name = "Ticket Subject")]
[MaxLength(30)]
[Required(ErrorMessage = "The ticket subject is required.")]
public string TicketSubject { get; set; }
[Display(Name = "Support Issue")]
[Min(1, ErrorMessage = "You must select a support issue.")]
public int SupportIssueID { get; set; }
[Display(Name = "Ticket Priority")]
[Min(1, ErrorMessage = "You must select a ticket priority.")]
public int TicketPriorityID { get; set; }
//public string EmployeeID { get; set; }
public bool IsClosed { get; set; }
[Required(ErrorMessage = "The detail message is required.")]
public string DetailMessage { get; set; }
}
查看代码:
@model RadixMVC.ViewModels.SupportViewModel
@{
ViewBag.Title = "Create New Support Ticket";
}
<h2>Radix Support: Create New Support Ticket</h2>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<fieldset style="width: 500px">
<legend>Create New Support Ticket</legend>
<div class="editor-label">
@Html.LabelFor(model => model.TicketSubject)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.TicketSubject)
@Html.ValidationMessageFor(model => model.TicketSubject)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.SupportIssueID)
</div>
<div class="editor-field">
@Html.DropDownList("SupportIssueID", string.Empty)
@Html.ValidationMessageFor(model => model.SupportIssueID)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.TicketPriorityID)
</div>
<div class="editor-field">
@Html.DropDownList("TicketPriorityID", string.Empty)
@Html.ValidationMessageFor(model => model.TicketPriorityID)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.IsClosed)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.IsClosed)
@Html.ValidationMessageFor(model => model.IsClosed)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.DetailMessage)
</div>
<div class="editor-field">
@*@Html.EditorFor(model => model.DetailMessage)*@
@Html.ValidationMessageFor(model => model.DetailMessage)
<br />
@{ Html.Telerik().EditorFor(model => model.DetailMessage)
.Name("DetailMessageEditor")
.HtmlAttributes(new { style = "height: 200px" })
.Encode(false)
.Render();
}
</div>
<div>
<br />
<input type="submit" value="Create Ticket" title="Submits a new support ticket" />
<input type="submit" onclick="parent.location='@Url.Action("Index", "Support", "Index")'" value="Cancel" title="Return to Support Home" />
</div>
</fieldset>
}
最后,控制器代码:
[HttpPost]
public ActionResult Create(SupportViewModel vm)
{
if (ModelState.IsValid)
{
SupportTicket SupportTicket = new SupportTicket()
{
SupportTicketID = Guid.NewGuid(),
EmployeeID = "123456",
TicketOpenDate = DateTime.Now,
TicketModifiedDate = DateTime.Now,
IsClosed = vm.IsClosed,
TicketSubject = vm.TicketSubject,
SupportIssueID = vm.SupportIssueID,
TicketPriorityID = vm.TicketPriorityID
};
TicketDetail TicketDetail = new TicketDetail()
{
TicketDetailID = Guid.NewGuid(),
SupportTicketID = SupportTicket.SupportTicketID,
TicketOrder = 1,
EmployeeID = "123456",
DetailDate = DateTime.Now,
DetailMessage = vm.DetailMessage
};
SupportTicket.TicketDetails.Add(TicketDetail);
db.SupportTickets.Add(SupportTicket);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.SupportIssueID = new SelectList(db.SupportIssues, "SupportIssueID", "Name", vm.SupportIssueID);
ViewBag.TicketPriorityID = new SelectList(db.TicketPriorities, "TicketPriorityID", "Name", vm.TicketPriorityID);
return View(vm);
}