开发者

MVC 3 EditorTemplate - Model properties are empty

I have a number of custom EditorTemplates for various model classes. Inside these templates I obviously need to reference the properties of the model. My problem is that when I use the direct syntax of Model.Id (for example), the value is null. Another example is Model.Name which retu开发者_StackOverflow中文版rns an empty string. However, when I reference the model in an expression (eg. @Html.TextBoxFor(i => i.Name)) then the values are there.

To further illustrate, here is a code snippet:

@model Vigilaris.Booking.Services.CompanyDTO
<div>
    <fieldset class="editfieldset">
        <legend class="titlelegend">Company Details</legend>
        <ol>
            <li>
               @Html.TextBox("tb1", @Model.Id) 
               @Html.TextBox("tb2", @Model.Name) 
            </li>
            <li>
                @Html.LabelFor(i => i.CreatedDate)
                @Html.DisplayFor(i => i.CreatedDate)
            </li>
            <li>
                @Html.LabelFor(i => i.Name)
                @Html.TextBoxFor(i => i.Name)
            </li>
            <li>
                @Html.LabelFor(i => i.Description)
                @Html.TextAreaFor(i => i.Description)
            </li>
            <li>
                @Html.LabelFor(i => i.Phone)
                @Html.TextBoxFor(i => i.Phone)
            </li>
        </ol>
    </fieldset>
</div>  

In this example, all the code that is using the LabelFor and DisplayFor helper functions displays the data from the model. However, the Html.TextBox code portion returns 0 for Model.Id and empty string for Name.

Why does it not access the actual model data when I reference Model directly?


I am unable to reproduce this. You might need to provide more context (controllers, views, ...). Also shouldn't your textbox be named like this:

@Html.TextBox("Id", Model.Id) 
@Html.TextBox("Name", Model.Name) 

and also why not using the strongly typed version directly:

@Html.TextBoxFor(x => x.Id) 
@Html.TextBox(x => x.Name) 


I managed to figure this one out. One thing I left out in my problem description was that I am using Telerik MVC Grid extension and the EditorTemplate is being using for In-form editing. So, the Model properties are not available at this point and this is understandable behaviour. I had to use a client side onEdit event on the Telerik MVC Grid and then set these values as necessary.

How I remember solving this is that I added a ClientEvent in my Telerik MVC Grid as follows:

.ClientEvents(events => events.OnEdit("Users_onEdit"))

This tells the grid to run my javascript function called Users_onEdit when an edit is triggered. Then, in my javascript function I find the field I want and then set its value. Here is an code excerpt:

function Users_onEdit(e) {
    if (e.mode == "insert") {
        $("#UserName").removeAttr("readonly");
        $("#UserName").removeAttr("title");
        $("#divNewUserMessage").show();

        var formId = String(e.form.id);
        var formIndex = formId.indexOf("form");
        var companyId = formId.substr(6, formIndex -6);
        var hiddenCompanyId = $(e.form).find("#CompanyId");
        hiddenCompanyId.val(companyId);
    }
}

I hope this helps others out there.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜