2012-08-29 41 views
2

我使用asp.net的MVC 3如何更新表單內容

有一種形式使Web應用程序:

@using (Html.BeginForm()) 
{ 
<div> 
    <fieldset> 
     <legend>Approvals</legend> 
     <div> 
      @Html.Label("Ticket ID") 
      @Html.DropDownList("TicketID") 
     </div> 
     <div> 
      @Html.Label("Distributor ID") 
      @Html.TextBox("DistributorID", null, new { @readonly = "readonly" }) 
     </div> 
     <div> 
      @Html.Label("Description") 
      @Html.TextArea("Description", new { @readonly = "readonly" }) 
     </div> 
     <div> 
      @Html.Label("Resolved") 
      @Html.DropDownList("Resolved") 
     </div> 
     <div> 
      @Html.Label("Date (yyyy/mm/dd)") 
      @Html.TextBox("Date") 
     </div> 
     <div> 
      @Html.Label("Time (HH:mm:ss)") 
      @Html.TextBox("Time") 
     </div> 
    </fieldset> 
    <input type="submit" value="Approve/Disapprove" /> 
</div> 
} 

如何從讀取值之後更新等領域的數據數據庫,只要從TicketID下拉列表中選擇一個票證ID。即每當票證ID改變時,相應地,在從特定票證ID的數據庫讀取數據而不提交表格的情況下,其他字段中的數據應該改變。

回答

1

你可以做到這一點使用AJAX:

$('select[name=TicketID]').change(function(){ 
    var selectedValue = $(this).find('option:selected').val(); 
    $.getJSON('/mycontroller/MyAction', selectedValue, 
    function(result){ 
     $('input[name=DistributorID]').val(result.DistributorID); 
     $('input[name=Description]').val(result.Description); 
     $('input[name=Resolved]').val(result.Resolved); 
     $('input[name=Date]').val(result.Date); 
     $('input[name=Time]').val(result.Time); 
    }); 
}); 

在控制器:

public JsonResult MyAction(int selectedValue) 
{ 
    var result = new 
    { 
     DistributorID = 1, 
     Description = 1, 
     Resolved= 1, 
     Date= 1, 
     Time= 1 
    }; 
    return Json(result, JsonRequestBehavour.AllowGet); 
} 

對不起,語法錯誤,我可能有,我寫的代碼直接在這裏

0

你可以調用一個返回jquery ajax post的json結果的動作。

這個動作返回你想要的東西。

$('#TicketID').change(function(){ 
    $.ajax({ 
    url: '/ActionThatReturnsJsonResult', 
    cache: false, 
    type: 'post' 
    success: function (data) { 
     $('#DistributorID').val(data.DistributorIDFromDataBase); 
    }, 
    error: function (e) { 
     alert(e.responseText);        
    } 
    }); 
});