Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am not able to get value in Controler..Please guide me if any thing wrong in code.

PAGE :-

XML
@model WCFWithMVC.Models.EmployeeModel

@{
    ViewBag.Title = "EmpDetail";

}

<script type="text/javascript">

//    $(document).ready(function () {
//        $('#BtnSubmit').click(function () {

//            var text = $('#RoleCode :selected').text();
//            alert(text);

//            var k = $('#Address').val();
//            alert(k);

//            var Seleted = $('#RoleCode').val();
//            alert(Seleted);

//
//        });

////        $("#RoleCode").change(function () {
////        $.ajax({
////           url: "Employee/ActionName",
////           type: 'POST',
////           cache: false,
////           data: { Selected: $("# Selected").val() },
////           success: function (data) {
////             //
////           }
////         });

//    });


    $(document).ready(function () {
        $("#RoleCode").change(function () {
            $.get("/Employee/ActionName", "Himan", function (data) {
                alert(data);
            });
        });
    });


    function somefunction() {
        $.ajax({
            url: 'Employee/ActionName'+"Himan", // SomeController, ActionName
            type: 'POST',
            data: "Himan",
            datatype:"json",
            success: function (data) {
                // Success Callback
                alert(data)
            }
        });
    }
</script>
<h2>EmpDetail</h2>

This is Employee Page

<table style="width:100%">
<tr>
<td style="width:20%;text-align:right">Employee:</td>
<td style="width:80%;text-align:left"> @Html.EditorFor(k => k.Address)</td>
</tr>
<tr>
<td style="width:20%;text-align:right">Salary:</td>
<td style="width:80%;text-align:left">@Html.EditorFor(k=>k.Salary)</td>
</tr>
<tr>
<td style="width:20%;text-align:right">Sex:</td>
@*<td style="width:80%;text-align:left"> @Html.DropDownListFor(k => k.RoleCode, ViewData["Rolenames"] as SelectListItem[], new { onchange = "somefunction();" })</td>*@
<td style="width:80%;text-align:left"> @Html.DropDownListFor(k => k.RoleCode, ViewData["Rolenames"] as SelectListItem[])</td>
</tr>
<tr>
<td style="width:20%;text-align:right">Add:</td>
<td style="width:80%;text-align:left">@Html.TextBox("Add")</td>
</tr>
<tr>
<td style="width:20%;text-align:right">Martial Status:</td>
<td style="width:80%;text-align:left">@Html.Label("Single")&nbsp;&nbsp; @Html.RadioButtonFor(k=>k.MartialStatus,"0")&nbsp;&nbsp @Html.Label("Married") &nbsp;&nbsp;@Html.RadioButtonFor(k=>k.MartialStatus,"1") </td>
</tr>
<tr>
<td style="width:20%;text-align:center" colspan="2"><input type="submit" id="BtnSubmit" value="Submit" /></td>
</tr>
</table>

JQUERY

<pre lang="php">$(document).ready(function () {
        $(&quot;#RoleCode&quot;).change(function () {
            $.get(&quot;/Employee/ActionName&quot;, &quot;Himan&quot;, function (data) {
                alert(data);
            });
        });
    });</pre>

CONTROLER:-

<pre lang="cs">public ActionResult ActionName(string Selected)
        {
            FillDDL();
            var result = new { Success = &quot;True&quot;, Message = &quot;Some Info&quot; };
            return Content(&quot;Himan&quot;);
        }

        public void FillDDL()
        {
            var listItems = new SelectListItem[] {
                            new SelectListItem(){Text=&quot;Corporate&quot;,Value=&quot;c&quot;},
                            new SelectListItem(){Text=&quot;Divisional&quot;,Value=&quot;d&quot;},
                            new SelectListItem(){Text=&quot;Regional&quot;,Value=&quot;r&quot;},
                            new SelectListItem(){Text=&quot;Facility&quot;,Value=&quot;f&quot;},
                            new SelectListItem(){Text=&quot;Resource&quot;,Value=&quot;s&quot;},
            };
            ViewBag.RoleNames = listItems;
        }</pre>
Posted
Updated 20-Nov-21 4:50am
Comments
Himanshu Yadav 14-Sep-13 0:19am    
public ActionResult ActionName(string Selected) in is this Selected coming null

I dont know for the PHP Jquery. But for the Javascript function Somefunction(), pass data as below.

data: { Selected: "Himan"},


And I think you don't need Himan in URL.

URL can also be written as

url: '@Url.Action("ActionName", "Employee")',



Try it and it should work fine.
 
Share this answer
 
you can use the sameparameter for the action method and the same parameter name where u passing to controler in View.
like
data: { Selected: "some text"}
 
Share this answer
 

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