2

这个编辑表单已经成功了,然后我想在这个表单中添加评论,为此我正在关注这篇文章

http://www.arrangeactassert.com/when-to-use-html-renderpartial-and-html-renderaction-in-asp-net-mvc-razor-views/

我创建了名为 CaseInternalEditViewModel 的视图模型

  public class CaseInternalEditViewModel
{
    public CaseInternalEditViewModel()
    {
        caseComment = new List<CaseComment>();
    }

    public String caseIDComment { get; set; }
    public CaseInternal caseInternal { get; set; }
    public List<CaseComment> caseComment { get; set; }
}

并在 View 中更改模型:

        @model myCHMTest.Models.CaseInternalEditViewModel

    @{
        ViewBag.Title = "Edit";
        Layout = "~/Views/Shared/_Layout.cshtml";
    }

    <h2>Edit</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>



       <div class="container">
        <fieldset>
        @using (Html.BeginForm())
    {
        @Html.ValidationSummary(true)
            <legend>CaseInternal</legend>
             <div class="container">
              @Html.HiddenFor(model => model.caseIDComment)
            @Html.HiddenFor(model => model.caseInternal.ID)
            @Html.HiddenFor(model => model.caseInternal.CaseID)
            @Html.HiddenFor(model => model.caseInternal.StatusID)
           @* @Html.HiddenFor(model => model.caseInternal.CreatedNIK)*@
            @Html.HiddenFor(model => model.caseInternal.CreatedBy)
            @Html.HiddenFor(model => model.caseInternal.CreatedDt)
            @Html.HiddenFor(model => model.caseInternal.SLA)
            @Html.HiddenFor(model => model.caseInternal.SLAFlag)
            @Html.HiddenFor(model => model.caseInternal.DatCreated)
            @Html.HiddenFor(model => model.caseInternal.DayResolved)
             @Html.HiddenFor(model => model.caseInternal.CategoryID)


            <div class="sixteen columns">            

                    <div class="fbbluebox">
                     <div class="editor-label">
                            @Html.LabelFor(model => model.caseInternal.CaseID)

                            @Html.DisplayFor(model => model.caseInternal.CaseID)
                            @Html.ValidationMessageFor(model => model.caseInternal.CaseID)
                        </div>
                         <div class="editor-label">
                            @Html.LabelFor(model => model.caseInternal.Title)
                        </div>
                        <div class="editor-field">
                            @Html.EditorFor(model => model.caseInternal.Title)
                            @Html.ValidationMessageFor(model => model.caseInternal.Title)
                        </div>

                    </div>
                    <hr />
              </div>        

              <div class="one-third column">



                    <div class="editor-label">
                        @Html.LabelFor(model => model.caseInternal.BranchID)
                    </div>
                    <div class="editor-field">
                        @Html.DropDownList("BranchID")
                        @Html.ValidationMessageFor(model => model.caseInternal.BranchID)
                    </div>



                    <div class="editor-label">
                        @Html.LabelFor(model => model.caseInternal.ProjectID, "InternalProject")
                    </div>
                    <div class="editor-field">
                        @Html.DropDownList("ProjectID", null, new { @onchange = "javascript:cascadingdropdown();" })
                        @Html.ValidationMessageFor(model => model.caseInternal.ProjectID)
                    </div>

                    <div class="editor-label">
                        @Html.LabelFor(model => model.caseInternal.SubProjectID, "InternalSubProject")
                    </div>
                    <div class="editor-field">
                        @Html.DropDownList("SubProjectID", String.Empty)
                       @*@Html.DropDownListFor(model => model.SubProjectID, new SelectList(Enumerable.Empty<SelectListItem>()))*@
                        @Html.ValidationMessageFor(model => model.caseInternal.SubProjectID)
                    </div>

                    <div class="editor-label">
                        @Html.LabelFor(model => model.caseInternal.PengaduanID, "InternalPPengaduan")
                    </div>
                    <div class="editor-field">
                        @Html.DropDownList("PengaduanID", String.Empty)
                        @Html.ValidationMessageFor(model => model.caseInternal.PengaduanID)
                    </div>
            </div>

            <div class="one-third column">                  

                    <div class="editor-label">
                        @Html.LabelFor(model => model.caseInternal.CreatedNIK)
                    </div>
                    <div class="editor-field">
                       @* @Html.TextBoxFor(model => model.caseInternal.CreatedNIK, new { disabled = "disabled" })
                        @Html.ValidationMessageFor(model => model.caseInternal.CreatedNIK)*@
                        <input type="text" name="InitialNIK" value="@Model.caseInternal.CreatedNIK" disabled="disabled"/>
                    </div>

                    <div class="editor-label">
                        @Html.LabelFor(model => model.caseInternal.CreatedBy)
                    </div>
                    <div class="editor-field">
                        @Html.TextBoxFor(model => model.caseInternal.CreatedBy, new { disabled = "disabled" })
                        @Html.ValidationMessageFor(model => model.caseInternal.CreatedBy)
                    </div>

                    <div class="editor-label">
                        @Html.LabelFor(model => model.caseInternal.CreatedDt)
                    </div>
                    <div class="editor-field">
                        @Html.TextBoxFor(model => model.caseInternal.CreatedDt, new { disabled = "disabled" })
                        @Html.ValidationMessageFor(model => model.caseInternal.CreatedDt)
                    </div>

                    <div class="editor-label">
                        @Html.LabelFor(model => model.caseInternal.AssignGroupID)
                    </div>
                    <div class="editor-field">
                        @Html.DropDownList("AssignGroupID", null, new { @onchange = "javascript:memberdropdown();" })
                        @Html.ValidationMessageFor(model => model.caseInternal.AssignGroupID)
                    </div>

                    <div class="editor-label">
                        @Html.LabelFor(model => model.caseInternal.AssignMemberID)
                    </div>
                    <div class="editor-field">
                         @Html.DropDownList("AssignMemberID", String.Empty)
                        @Html.ValidationMessageFor(model => model.caseInternal.AssignMemberID)
                    </div>

                    <div class="editor-label">
                        @Html.LabelFor(model => model.caseInternal.StatusID)
                    </div>
                    <div class="editor-field">
                        @Html.DropDownList("StatusID",null, new { @disabled = "disabled" })
                        @Html.ValidationMessageFor(model => model.caseInternal.StatusID)
                    </div>
                </div>

                 <div class="one-third column">
                    <div class="editor-label">
                        @Html.LabelFor(model => model.caseInternal.SLA)
                    </div>
                    <div class="editor-field">
                        @Html.TextBoxFor(model => model.caseInternal.SLA, new { disabled = "disabled" })
                        @Html.ValidationMessageFor(model => model.caseInternal.SLA)
                    </div>

                    <div class="editor-label">
                        @Html.LabelFor(model => model.caseInternal.SLAFlag)
                    </div>
                    <div class="editor-field">
                        @Html.TextBoxFor(model => model.caseInternal.SLAFlag, new { disabled = "disabled" })
                        @Html.ValidationMessageFor(model => model.caseInternal.SLAFlag)
                    </div>

                    <div class="editor-label">
                        @Html.LabelFor(model => model.caseInternal.DayResolved)
                    </div>
                    <div class="editor-field">
                        @Html.TextBoxFor(model => model.caseInternal.DayResolved, new { disabled = "disabled" })
                        @Html.ValidationMessageFor(model => model.caseInternal.DayResolved)
                    </div>

                    <div class="editor-label">
                        @Html.LabelFor(model => model.caseInternal.DatCreated)
                    </div>
                    <div class="editor-field">
                        @Html.TextBoxFor(model => model.caseInternal.DatCreated, new { disabled = "disabled" })
                        @Html.ValidationMessageFor(model => model.caseInternal.DatCreated)
                    </div>
            </div>

             <div class="sixteen columns">
                <div class="fbgreybox"  >
                     <div class="editor-label">
                        @Html.LabelFor(model => model.caseInternal.Description)
                    </div>
                    <div class="editor-field">
                        @Html.TextAreaFor(model => model.caseInternal.Description, new { style = "width: 100%; height: 100px;" })
                        @Html.ValidationMessageFor(model => model.caseInternal.Description)
                    </div>

                    <div class="editor-label">
                        @Html.LabelFor(model => model.caseInternal.LinkCase)
                    </div>
                    <div class="editor-field">
                        @Html.EditorFor(model => model.caseInternal.LinkCase)
                        @Html.ValidationMessageFor(model => model.caseInternal.LinkCase)
                    </div>
                </div>              
                        <p>
                            <input type="submit" value="Save" class="uibutton large confirm"/>
                            @Html.ActionLink("Back to List", "Index", "CaseInternal", new { @class = "uibutton" })
                        </p>    

           </div>    
             </div>
    } 
        </fieldset>
         </div>


    @using (Ajax.BeginForm("Comment", "CaseInternal", new AjaxOptions { InsertionMode = InsertionMode.Replace, UpdateTargetId = "myGrid", OnSuccess = "done" }))
    {
    @*<input type="text" id="caseEka" name="caseEka" />*@
          @Html.HiddenFor(model => model.caseIDComment)
    <input type="text" id="Comment" name="Comment"/>
    }

        <div id="myGrid">
        @Html.Partial("_showComment", Model.caseComment)
        </div>

    <script type="text/javascript">
        function done() {

            document.getElementById('Comment').value = '';
        }

        function cascadingdropdown() {
            var idDept = $("#ProjectID").val();
            var subProject = $('#SubProjectID').val();
            var urlemp = '@Url.Action("GetSubProjectFromProjectID")';
            var select = $('#SubProjectID');
            $.ajax({
                type: "POST",
                url: urlemp,
                data: { id: idDept },
                error: function (jqXHR, textStatus, errorThrown) {
                    alert("error" + jqXHR.responseText);
                },
                success: function (returndata) {
                    if (returndata.ok) {                  
                        select.empty();
                        $.each(returndata.data, function (index, itemData) {

                            if (subProject == itemData.ID) {
                                select.append($('<option selected="selected"></option>').val(itemData.ID).html(itemData.SubProjectName));
                            } else {
                                select.append($('<option></option>').val(itemData.ID).html(itemData.SubProjectName));
                            }

                        });
                        select.show('slow');
                        //                    $("#ProjectID").attr("disabled", "disabled");
                    }
                    else {
                        window.alert(' error : ' + returndata.message);
                    }
                }
            }
            );
        }

        function getNameNik() {
            window.alert("AAAASSDSD");
            var idDept = $("#CreatedNIK").val();
            var urlemp = '@Url.Action("GetNameNik")';
            $.ajax({
                type: "POST",
                url: urlemp,
                data: { id: idDept },
                error: function (jqXHR, textStatus, errorThrown) {
                    alert("error" + jqXHR.responseText);
                },
                success: function (returndata) {
                    if (returndata.ok) {
                        $('#nikLabel').text("");
                        $.each(returndata.data, function (index, itemData) {
                            $('#nikLabel').text(itemData.FirstName + " " + itemData.LastName);
                        });
                    }
                    else {
                        window.alert(' error : ' + returndata.message);
                    }
                }
            }
            );
        }

        function memberdropdown() {
            var idDept = $("#AssignGroupID").val();
            var subProject = $('#AssignMemberID').val();
            var urlemp = '@Url.Action("GetMemberFromGroupID")';
            var select = $('#AssignMemberID');
            $.ajax({
                type: "POST",
                url: urlemp,
                data: { id: idDept },
                error: function (jqXHR, textStatus, errorThrown) {
                    alert("error" + jqXHR.responseText);
                },
                success: function (returndata) {
                    select.empty();
                    if (returndata.ok) {

                        $.each(returndata.data, function (index, itemData) {
                            if (subProject == itemData.ID) {                           
                                select.append($('<option selected="selected"></option>').val(itemData.ID).html(itemData.UserName));
                            } else {
                                select.append($('<option></option>').val(itemData.ID).html(itemData.UserName));
                            }

                        });
                        select.show('slow');
                        //                    $("#ProjectID").attr("disabled", "disabled");
                    }
                    else {
                        window.alert(' error : ' + returndata.message);
                    }
                }
            }
            );
        }

        function start() {
            cascadingdropdown();
            memberdropdown();
        }

        window.onload = start;
    //    window.onload = memberdropdown;
    </script>

这里是我的控制器:

 public ActionResult Edit(string id)
    {
        CaseInternal caseinternal = db.CaseInternals.Find(id);
        caseinternal.NullSafeTrimStrings();
        ViewBag.PengaduanID = new SelectList(db.InternalPPengaduans, "ID", "PPengaduanName", caseinternal.PengaduanID);
        ViewBag.ProjectID = new SelectList(db.InternalProjects, "ID", "ProjectName", caseinternal.ProjectID);
        ViewBag.SubProjectID = new SelectList(db.InternalSubProjects, "ID", "SubProjectName", caseinternal.SubProjectID);
        ViewBag.CategoryID = new SelectList(db.MasterCategories, "ID", "CategoryName", caseinternal.CategoryID);           
        ViewBag.AssignGroupID = new SelectList(db.MasterGroups, "ID", "GroupName", caseinternal.AssignGroupID);
        ViewBag.AssignMemberID = new SelectList(db.MasterAssignUsers, "ID", "UserName", caseinternal.AssignMemberID);
        ViewBag.BranchID = new SelectList(branchObject.A2BR, "BRCODE", "BRNAME", caseinternal.BranchID);
        ViewBag.StatusID = new SelectList(db.MasterStatus, "ID", "StatusName", caseinternal.StatusID);
        CaseInternalEditViewModel caseInternalEdit = new CaseInternalEditViewModel();
        caseInternalEdit.caseInternal = caseinternal;
        caseInternalEdit.caseIDComment = caseinternal.CaseID;
        var commentCase = db.CaseComments.Where(p => p.CaseID == caseinternal.CaseID).OrderByDescending(p => p.CreatedDt);
        foreach (CaseComment cas in commentCase)
        {
            caseInternalEdit.caseComment.Add(cas);
        }
        return View(caseInternalEdit);
    }

[HttpPost]
    public ActionResult Edit(CaseInternalEditViewModel caseinternalEdit)
    {
        //CaseInternalEditViewModel caseinternalEdit
        CaseInternal caseinternal = caseinternalEdit.caseInternal;
        if (ModelState.IsValid)
        {
            db.Entry(caseinternal).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        ViewBag.PengaduanID = new SelectList(db.InternalPPengaduans, "ID", "PPengaduanName", caseinternal.PengaduanID);
        ViewBag.ProjectID = new SelectList(db.InternalProjects, "ID", "ProjectName", caseinternal.ProjectID);
        ViewBag.SubProjectID = new SelectList(db.InternalSubProjects, "ID", "SubProjectName", caseinternal.SubProjectID);
        ViewBag.CategoryID = new SelectList(db.MasterCategories, "ID", "CategoryName", caseinternal.CategoryID);
        ViewBag.AssignGroupID = new SelectList(db.MasterGroups, "ID", "GroupName", caseinternal.AssignGroupID);
        ViewBag.AssignMemberID = new SelectList(db.MasterAssignUsers, "ID", "UserName", caseinternal.AssignMemberID);
        ViewBag.BranchID = new SelectList(branchObject.A2BR, "BRCODE", "BRNAME", caseinternal.BranchID);
        ViewBag.StatusID = new SelectList(db.MasterStatus, "ID", "StatusName", caseinternal.StatusID); 
        return View(caseinternalEdit);
    }

模型状态始终无效,因为某些字段为空,下拉列表中的 field2 在传递给控制器​​时为空。

是viewbag的问题吗?我应该更改 viewbag 的名称吗?

 @Html.EditorFor(model => model.caseInternal.Title)

呈现为

<input class="text-box single-line" id="caseInternal_Title" name="caseInternal.Title" type="text" value="ViewModel" />

但是下拉列表

 <select id="BranchID" name="BranchID"><option selected="selected" value="001">KANTOR PUSAT NON OPERASIONAL  </option>

不同的是名称,分支应该是 caseinternal.branchID 也许它会工作,但怎么做呢?

4

1 回答 1

1

你有:

@Html.DropDownList("BranchID")

你要:

@Html.DropDownListFor(model => model.caseInternal.BranchID)

或者,如果您出于某种原因真的不想使用 DropDownListFor() ......

@Html.DropDownList("caseInternal_BranchID")
于 2013-06-12T20:46:09.147 回答