2014-02-14 82 views
0

這非常奇怪,我知道這在過去有效。我必須用別的東西打破它,但我真的不知道是什麼。編輯表單中的MVC 4下拉列表不會在編輯命令之後給出正確的值

我是一個很少有MVC知識的.NET程序員,似乎我在這裏錯過了一些關鍵的東西,即mvc在後臺做的事情。

讓我們儘可能地解釋它。

我有一個觀點,其中包含此:

<tbody> 
     @foreach (var experienceViewModel in this.ViewData.Model) 
     { 
      var id = "GetExperienceDetail" + experienceViewModel.Id; 

      <tr> 
       <td>@experienceViewModel.Assessment</td> 
       <td>@experienceViewModel.CompanyName</td> 
       <td>@experienceViewModel.Description</td> 
       <td>@experienceViewModel.StartDate</td> 
       <td>@experienceViewModel.EndDate</td> 
       <td width="20"><a href="#" id="@id" onclick="openDetail('@id')" data-   controller="Person" data-action="ExperienceDetail" data-key="@experienceViewModel.Id" data-person="@experienceViewModel.PersonId" data-form="ExperienceDetail" title="Wijzig" class="btn btn-xs"><i class="glyphicon glyphicon-pencil"></i></a></td> 
       <td width="20"><a id="@Html.Raw(id + "delete")" class="confirm-delete" data-keyname="experienceId" data-textfield="@string.Concat(experienceViewModel.Assessment, " | ", experienceViewModel.Description)" data-link="@Html.Raw(id + "delete")" data-key="@experienceViewModel.Id" data-controller="Experience" data-action="ExperienceDelete" href="#" title="Verwijder"><i class="glyphicon glyphicon-remove"></i></a></td> 

      </tr>      
     } 

它不是最後TR但在此之前的一個。一個與openDetail。此功能會從JS web.js這我下面要貼被稱爲:

function openDetail(link) { 
var $link = $('#' + link); 

debugger; 

var controller = $link.attr('data-controller'); 
var action = $link.attr('data-action'); 
var id = $link.attr('data-key'); 
var form = $link.attr('data-form'); 
var modelName = $link.attr('data-model'); 

//default person 
if (modelName == null) 
    modelName = "person"; 

modelName = modelName.toLowerCase(); 

var valueModel = $link.attr('data-' + modelName); 




var json = "{\"id\" : " + id + ", \"" + modelName + "\"" + " : " + valueModel + "}"; 
var url = '/' + controller + '/' + action; 

$.ajax({ 
    url: '/' + controller + '/' + action, 
    type: 'POST', // not sending json over url 
    data: json, 
    contentType: "application/json; charset=utf-8", 
    success: function (data) { 

     $('#partialoveview').html(data); 

     if (typeof form !== 'undefined' && form !== false) { 
      $.validator.unobtrusive.parse($('#' + form)); 
      $("span.field-validation-valid").hide(); 
      $('.label-error').hide(); 
     } 

     loadForm(); 


    }, 
    error: function (data) { 

    } 
}); 

這個函數做的工作,這是怎麼了?是下拉列表的唯一的事情,尤其一個原因有另外一個,在它下面,這有選擇,它具有價值的正確行爲......

這將去人控制器的動作ExperienceDetail(INT ID,詮釋人)

 [HttpPost, OutputCacheAttribute(VaryByParam = "*", Duration = 0, NoStore = true)] 
    public PartialViewResult ExperienceDetail(int id, int person) 
    { 
     var experienceViewModel = new ExperienceViewModel { AssessmentList = this.AssessmentList(), CompanyList = this.CompanyList(), PersonId = person }; 


     if (id > 0) 
     { 
      Data.Model.Experience experience = this._experienceRepository.GetExperienceDetail(id); 
      experienceViewModel = new ExperienceViewModel(experience) { AssessmentList = this.AssessmentList(), CompanyList = this.CompanyList()}; 
     } 

     return this.PartialView("_ExperienceDetailPartial", experienceViewModel); 
    } 

this.CompanyList從basecontrol中獲取一個選擇列表ler 當這個視圖返回時,我不能看到這個選擇列表有一個選定的值。但是,然後再次,正在工作的ddl也在選定的值爲null,因此在編輯後我無法設置正確的值。

然後將細節畫面,其放入一個div從JS: 我只把與第一家公司開始降下來(不工作),然後評估DDL(工作)

@model Emenka.HumanResources.Application.Models.ExperienceViewModel 

@using (Html.BeginForm("UpdateExperienceDetail", "Person", FormMethod.Post, new { @id = "ExperienceDetail", @role = "form", data_action = "ExperienceOverview", data_controller = "Person" })) 
{ 
@Html.HiddenFor(m => m.Id) 
@Html.HiddenFor(m => m.PersonId) 

<fieldset> 

<legend>Ervaring</legend> 

<div class="row"> 

    <div class="col-md-5"> 

    <div class="form-group"> 
     @Html.LabelFor(m => m.CompanyId, new { @class = "control-label" }) 
     <div class="input-group col-xs-9"> 
     @Html.DropDownListFor(m => m.CompanyId, ViewData.Model.CompanyList, string.Empty, new { @class = "no-focus form-control" }) 
     @*&nbsp;@Html.ValidationMessageFor(m => m.CompanyId, " ")*@ 
     <a href="#modalAdd" role="button" class="input-group-addon btnModal" data- toggle="modal" data-target="#modalAdd" data-controller="Person" data-action="CreateCompany" data-controlid="CompanyId" data-modaltype="Company">...</a> 
     </div> 
    </div> 

    <div class="form-group"> 
     @Html.LabelFor(m => m.AssessmentId, new { @class = "control-label" }) 
     @Html.DropDownListFor(m => m.AssessmentId, ViewData.Model.AssessmentList, string.Empty, new { @class = "no-focus control-sm-7 form-control" }) 
     @*&nbsp;@Html.ValidationMessageFor(m => m.AssessmentId, " ")*@ 
    </div> 

    <div class="form-group"> 
     @Html.LabelFor(x => x.StartDate, new { @class = "control-label" }) 
     <div class="input-group col-xs-7"> 
     @Html.TextBoxFor(x => x.StartDate, new { @class = "form-control date-picker", @readonly = "readonly" }) 
     <label for="StartDate" class="input-group-addon glyphicon glyphicon-calendar add-on" /> 
     </div> 
    </div> 

我搜索和搜索,我只是不知道要搜索什麼。你們可以給我提示plz嗎? PS:不要介意在數據attrib空間,我試圖擺脫錯誤時提交此表單在stackoverflow(代碼4空格)。

我知道從下拉菜單的第三個參數是選擇默認值,但這是string.Empty與評估的下拉列表,這點擊編輯時得到正確的值,所以不是像公司這樣的空字符串下拉...所以在邏輯上這不可能是問題

+0

期待更多的迴應在這裏:( – user3309318

+0

爲什麼沒有人迴應這個? – user3309318

回答

0

我發現了問題!

設置鏈接到下拉列表的相應companyId的viewdata屬性,解決了我所有的問題。我不知道爲什麼這在其他網頁工作,也許我只是錯過了它。