1

我目前正在开发我的第一个 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);
    }
4

2 回答 2

3

我能够得到这个工作。该文档要么非常过时,要么只是没有解释如何很好地做到这一点(可能两者兼而有之)。但是我能够通过对我的 Razor 语法进行以下更改来完成这项工作:

        <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(true)
               .Render();
        } 
    </div>

从控件中删除“名称”属性解决了无法取回任何东西的问题,但是当我尝试保存时,我立即收到错误(与 XSS、跨站点脚本有关),我认为是因为HTML 没有被编码。我将 Encode 属性更改为 true,现在一切正常。

于 2012-04-09T00:37:37.343 回答
0

今天遇到了类似的事情。

我使用的是视图模型,而我想绑定的属性是视图模型中子对象的属性。

当我提交时,RTE 中的值没有被绑定。当我查看 Request.From 对象时,我可以看到该值以正确的格式返回,以便以通常的方式绑定,所以我有点困惑。

无论如何,如果您希望它绑定,您需要将您的财产的确切名称提供给 RTE,所以在您的情况下

.Name("DetailMessage") 

应该工作,但

.Name("DetailMessageEditor") 

将不会。

我的情况是我必须命名 RTE

.Name("object.Property") 

其中 object 是我的财产所在的 View 模型上的子对象,以使其工作

希望这可以帮助某人。

于 2012-05-28T10:51:57.277 回答