2011-02-12 38 views
0

我正在使用jquery函數從asp.net mvc 3控制器獲取一些信息到我的視圖,使用json。使用jquery修改中的文本並向mvc3控件回調

查看:

<script type="text/javascript"> 
$(document).ready(function(){ 
    $("#CraftID").change(function() { 
     $.post("/Element/GetItems/", { CraftID: $(this).val(), ElementID: $("#ElementID").val() }, function (data) { 
      populateDropdown($("#ItemID"), data); 
     }); 
    }); 

    $("#ItemID").change(function() { 
     $.post("/Element/GetUnit/", { ItemID: $(this).val(), ElementID: $("#ElementID").val() }, function (units) { 
      appendUnit($("#factor"), units); 
     }); 
    }); 

    $("#btn_next").click(function() { 
     window.location = "/Element/"; 
    }); 

}); 

function populateDropdown(select, data) { 
    select.html(''); 
    $.each(data, function (id, option) { 
     select.append($('<option></option>').val(option.ItemID).html(option.Description)); 
    }); 
} // } inserted here 

function appendUnit(factor, units) { }; 
} // } removed here 
</script> 

而且在剃刀HTML:

<div class="editor-field"> 
     @Html.DropDownList("CraftID", new SelectList(ViewBag.Crafts as System.Collections.IEnumerable, "CraftID", "Description"))   
    </div> 

    <div class="editor-label"> 
     Leitposition: 
    </div>   
    <div class="editor-field"> 
     <select id="ItemID" name="ItemID"></select> 
    </div> 

    <div class="editor-label" id="factor"> 
     Umrechnungsfaktor: 
    </div> 

我CONTROLER看起來是這樣的:

public JsonResult GetUnit(int itemid, int elementid) 
    { 
     try 
     { 
      Element element = db.Elements.Single(e => e.ElementID == elementid); 

      Item item = db.Items.Single(it => it.ItemID == itemid); 

      ItemViewModel ivm = new ItemViewModel(item); 

      ElementViewModel evm = new ElementViewModel (element); 

      string str_unit = "["+ ivm.Unit_toString() + "/" + evm.Unit_toString() + "]"; 

      return Json(str_unit); 
     } 
     catch (Exception) 
     { 
      return null; 
     } 
    } 

因此,繼承人的問題: 當我來到本網站我的IDE會拋出以下內容:

Laufzeitfehler Microsoft JScript中:OBJEKT erwartet

Runtimeerror Microsoft JScript中:預期的對象

和線路:appendUnit($( 「#因子」),單位);突出顯示。我真的沒有得到什麼問題,因爲調試器發現正確的<div>。我知道,因爲當我鼠標懸停功能appendUnit(factor...它顯示我innerHTML:「Umrechnungsfaktor:」。

所以這就是問題,現在我想要做的事情: 我只是想渲染我的<div class="editor-label" id=factor> Umrechnungsfaktor + units </div> 單位來自我的控制器GetUnit。

功能appendUnit(因子,單位)是空的用於測試becouse我不能發現它應該是這樣的錯誤:

function appendUnit(factor, units) { 
    factor.html = ' '; 
    factor.append("Umrechnungsfaktor: " + units.val()); 
}; 
+0

你確認了對`GetUnit`的調用是否返回結果? – Jacob 2011-02-12 02:15:47

回答

0

下功能appendUnit() {}}包括FUNC到功能塊populatedropdown (select, data) {...}。沒有什麼錯誤,影響很大,不容易追蹤。我在我的文章中糾正了它,也許它對某人有用;)謝謝