加载视图模型时,不会填充下拉框

时间:2014-08-08 17:10:55

标签: c# jquery asp.net-mvc

我正在使用带有Entity Framework的C#ASP.NET MVC,尝试使用部分视图和对话框创建一个不需要在一个“页面”上的Web应用程序。

索引页面显示一个表格,并且有jquery按钮链接到对话框,其中包含用于处理内容的部分视图。

我目前正在处理负责在表格中添加和编辑实体的部分视图。它添加它们很好,部分工作很好,但是当我尝试将一个对象加载到视图模型中时,事情就会出错。实体本身是完全完整的,不用担心,但并非所有信息都加载在局部视图中,其中一些信息格式错误。具体地,

The edit dialog which is causing issues

应填写所有这些下拉框。它们是填充的,只是没有正确的值。此外,viewmodel中的代码应该阻止日期显示;

[Required]
[Display(Name = "Date Requested")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:dd MMM yyyy}")]
public System.DateTime DateRequested { get; set; }

显示部分视图的控制器操作是这样的;

public ActionResult _advisoryRequestForm(string id = "")
{
    ViewBag.datepickeruid = Guid.NewGuid();
    setViewBagNewAR();

    if (id.Equals(""))
        return PartialView();
    BCRTAdvisoryRequest request = new AdvisoryRequestsLogic().getAdvisoryRequestByID(Convert.ToInt32(id));      
    return PartialView(request);
}

ajax电话;

$(".editRequest").button().on("click", function () {        
    $.ajax({
        url: 'Home/_advisoryRequestForm',
        type: 'POST',
        data: "&id="+this.id,
        success: function (response) {
            if (response) {
                $('#ardialog-form').html(response);
                requestForm = $('#ardialog-form').find('#advisoryRequestForm');
                $.validator.unobtrusive.parse(requestForm);
                editRequestDialog.dialog("open");
            }
        },
        error: function (xhr, ajaxOptions, thrownError) { alert(xhr.status); alert(thrownError); }
    });
});

返回部分视图;

@model EH.BCRT.AdvisoryRequests.Model.BCRTAdvisoryRequest

<link href="@Url.Content("/Content/Site.css")" rel="stylesheet" />
<link href="@Url.Content("/Content/themes/base/jquery.ui.dialog.css")" rel="stylesheet" />
<link href="@Url.Content("/Content/themes/base/jquery.ui.datepicker.css")" rel="stylesheet" />

<form id="advisoryRequestForm">
    @using (Html.BeginForm())
 {
        @Html.AntiForgeryToken()
        @Html.ValidationSummary(true)

        <fieldset>
            <legend>BCRTAdvisoryRequest</legend>
            <div class="container">
                <div class="h-double">
                    <div class="editor-label">
                        @Html.LabelFor(m => m.RequestTypeID)
                    </div>
                    <div class="editor-field">
                        @Html.ValidationMessageFor(model => model.RequestTypeID)<br />
                        @Html.DropDownListFor(m => m.RequestTypeID, (IEnumerable<SelectListItem>)ViewBag.RequestTypeID, "", null)
                    </div>

                    <div class="editor-label">
                        @Html.LabelFor(m => m.DateRequested)
                    </div>
                    <div class="editor-field">
                        @Html.ValidationMessageFor(model => model.DateRequested)<br />
                        @Html.TextBoxFor(model => model.DateRequested, new { @class = "datepickerfield", id = ViewBag.datepickeruid })
                    </div>

                    <div class="editor-label">
                        @Html.LabelFor(model => model.SiteName)
                    </div>
                    <div class="editor-field">
                        @Html.ValidationMessageFor(model => model.SiteName)<br />
                        @Html.EditorFor(model => model.SiteName)
                    </div>

                    <div class="editor-label">
                        @Html.LabelFor(model => model.ServiceCategoryID)
                    </div>
                    <div class="editor-field">
                        @Html.ValidationMessageFor(model => model.ServiceCategoryID)<br />
                        @Html.DropDownListFor(m => m.ServiceCategoryID, (IEnumerable<SelectListItem>)ViewBag.ServiceCategoryID, "", null)
                    </div>

                    <div class="editor-label">
                        @Html.LabelFor(model => model.Description)
                    </div>
                    <div class="editor-field">
                        @Html.ValidationMessageFor(model => model.Description)<br />
                        @Html.TextAreaFor(model => model.Description, new { style = "width:95%;min-height:80px;" })
                    </div>
                </div>
                <div class="h-double">
                    <div class="editor-label">
                        @Html.LabelFor(model => model.RequestedBy)
                    </div>
                    <div class="editor-field">
                        @Html.ValidationMessageFor(model => model.RequestedBy)<br />
                        @Html.EditorFor(model => model.RequestedBy)
                    </div>

                    <div class="editor-label">
                        @Html.LabelFor(model => model.JobTitle)
                    </div>
                    <div class="editor-field">
                        @Html.ValidationMessageFor(model => model.JobTitle)<br />
                        @Html.EditorFor(model => model.JobTitle)
                    </div>

                    <div class="editor-label">
                        @Html.LabelFor(model => model.LocalOfficeOrTeam)
                    </div>
                    <div class="editor-field">
                        @Html.ValidationMessageFor(model => model.LocalOfficeOrTeam)<br />
                        @Html.EditorFor(model => model.LocalOfficeOrTeam)
                    </div>

                    <div class="editor-label">
                        @Html.LabelFor(model => model.SiteVisit)
                    </div>
                    <div class="editor-field">
                        @Html.ValidationMessageFor(model => model.SiteVisit)<br />
                        @Html.EditorFor(model => model.SiteVisit)
                    </div>

                    <div class="editor-label">
                        @Html.LabelFor(model => model.StatusID)
                    </div>
                    <div class="editor-field">
                        @Html.ValidationMessageFor(model => model.StatusID)<br />
                        @Html.DropDownListFor(m => m.StatusID, (IEnumerable<SelectListItem>)ViewBag.StatusID, "", null)
                    </div>

                    <div class="editor-label">
                        @Html.LabelFor(model => model.ConsultantRetained)
                    </div>
                    <div class="editor-field">
                        @Html.ValidationMessageFor(model => model.ConsultantRetained)<br />
                        @Html.EditorFor(model => model.ConsultantRetained)
                    </div>
                </div>
                <div class="h-single">
                    <div class="editor-label">
                        @Html.LabelFor(model => model.Comments)
                    </div>
                    <div class="editor-field">
                        @Html.ValidationMessageFor(model => model.Comments)<br />
                        @Html.TextAreaFor(model => model.Comments, new { style = "width:95%;min-height:80px;" })
                    </div>
                </div>
            </div>
        </fieldset>
 }
</form>
<script type="text/javascript">
    $(document).ready(function () {
        $('.datepickerfield').each(function () {
            $(this).datepicker({ dateFormat: 'dd-mm-yy' });
        });
    });
</script>

关于下拉列表和日期字段可能出错的任何想法?

setViewBagNewAR()

private void setViewBagNewAR()
{
    var ARLogic = new AdvisoryRequestsLogic();
    ViewBag.StatusID = ARLogic.getRequestStatuses();
    ViewBag.RequestTypeID = ARLogic.getRequestTypes();
    ViewBag.ServiceCategoryID = ARLogic.getServiceCategories();
}

1 个答案:

答案 0 :(得分:1)

ViewBag中的SelectLists的名称应与所有模型属性的名称不同。

例如,您可以将状态ID的控制器重写为:

    ViewBag.StatID = ARLogic.getRequestStatuses();

然后,将View中的DropDownlistFor更改为

    @Html.DropDownListFor(m => m.StatusID, (SelectList)ViewBag.StatID, "", null)

对于日期格式,有一个ApplyFormatInEditMode属性,它将格式应用于EditorFor中的日期。

    [DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}", ApplyFormatInEditMode = true)]