Click here to Skip to main content
15,896,730 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
C#
Hi ,
i have a Fuel table with following fields :

ColumnName:     
1.BillNo    2.BillDate    3.Amount  4.EmployeeId
1001        24.02.2015    1000         e101
1002        25.02.2016     586         e102      

and also i bind the dropdownlist with employee tale with following fields
1.Employee Id(Selected value)
2.Employee Name(Display Value)

In My View I have DDL_Employee(dropdownlist) ,txt_fuel(textbox)
Now my question is While i am selecting DDL_Employee value(E101) Amount of 1000 will binidng in txt_Fuel

How to do this Task in Asp.Net Mvc, i am familiar with c# windows application, but here i am stuck in asp.net MVC ,
Give Any Suggestion


What I have tried:

i am trying in this coding :

View:

<script type="text/javascript">

function Action(empName) {
$.ajax({
url:'@Url.Action("FillAdvanceDetails", "AdvancePaymentDetails")',
type:"POST",
data: { "empId": empId },
"Success":function(data){
if (data!=null) {
var vdata=data;
$('#fuelTotalAmount').val(vdata[0].empId);

}
}

});




}

</script>

Controller :

public ActionResult FillAdvanceDetails(Guid empId)
{
var advanceDetials = (from f in db.PettyCashes where f.PettyCashTypeID.ToString()== empId.ToString () select f).ToList();
return Json(advanceDetials);

}
Posted
Updated 23-Feb-16 18:54pm

1 solution

Try with below steps:

Step 1: Code for Action
C#
[HttpPost]
public ActionResult FillAdvanceDetails(string empId)
{
	var advanceDetials = (from f in db.PettyCashes where f.PettyCashTypeID.ToString() == empId select f).ToList();
	return Json(advanceDetials);

}

Step 2: Code for Javascript
JavaScript
<script src="~/Scripts/jquery-1.8.2.min.js"></script>
<script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>

<script>
    var jsonObj = { "empId": $("#DDL_Employee option:selected").val())  };

    $.ajax({
        url: '@Url.Action("FillAdvanceDetails", "AdvancePaymentDetails")',
        type: "POST",
        data: JSON.stringify(jsonObj),
        contentType: "application/json",
        datatype: "json",
        success: function (data) {
            if (data != null) {
                var vdata = data;
                $('#fuelTotalAmount').val(vdata.empId);
            }
        }
    });
</script>
 
Share this answer
 
v2
Comments
Nithu Nithiya 24-Feb-16 1:37am    
@Html.DropDownList("Employee", (SelectList)ViewData["empName"], new { onchange = "Action(this.value);", @class = "form-control" })
It is my Dropdownlist ,
In this DDL, which obj name i mentioned inside of json obj
var jsonObj = { "empId": $("#DDL_Employee option:selected").val()) };
[no name] 24-Feb-16 1:43am    
See, if you are getting correct value in Action(empName) then there is no need to use code like: $("#DDL_Employee option:selected").val()). You can use directly
var jsonObj = { "empId": empName };

Secondly in you javascript code you are passing only empName but using empId which creates confuse to me. Use variables as per your need.

Do not confuse.
Nithu Nithiya 4-Mar-16 2:57am    
i am trying with same code with few modification based on my requirements, it works fine , Thank you @Manas_Kumar

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900