Click here to Skip to main content
15,885,309 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am new to asp.net and trying to create Customer Info Form ,now in a custController i successfully create a country list and as parameter ContryID i wand to creat public actionResult Getstate but it should not Works

What I have tried:

in a custController

public List<Country> GetCountries() {
         List<Country> countries = dc.Countries.ToList();
         return countries;
     }

     public ActionResult GetState(int CountryID) {
          List<State_list> states = dc.State_list.Where(x => x.CountryID == CountryID).ToList();
          ViewBag.stList = new SelectList(states, "StatelistID", "STATE", "CountryID");

         return PartialView("DisplayState");

     }
     [HttpGet]
     public ActionResult Create() {

         ViewBag.ContList = new SelectList(GetCountries(), "CountryID", "CountryName");
         return View();
     }
 }



In a
PartialView("DisplayState")


@model ChaBuss.Models.State_list
	<option value="">-- Select States --</option>
@if (ViewBag.stList != null) {
	foreach (var item in ViewBag.stList) {

		<option value="@item.value">@item.text</option>
	}
}



in View Html

@model ChaBuss.Models.Customer

@{
	ViewBag.Title = "Create";
}

<h4 style="font:300;color:#0026ff">Create New Customer </h4>
<html>
<head>




</head>
<body>
	<form actions="#">
		<div class="form-horizontal">
			<hr />
			@Html.ValidationSummary(true, "", new { @class = "text-danger" })
			<div class="form-group">

				<label class="control-label">Customer name </label>   
				@*	@Html.LabelFor(model => model.Customer_Name, htmlAttributes: new { @class = "control-label col-md-2" })*@
				<div colspan="3">
					@Html.EditorFor(model => model.Customer_Name, new { htmlAttributes = new { @class = "form-control" } })
					@Html.ValidationMessageFor(model => model.Customer_Name, "", new { @class = "text-danger" })
				</div>
			</div>
			<div class="form-group">
				<label class="control-label">Customer Address </label>
				<div colspan="3">
					@Html.TextAreaFor(model => model.CustomerAddress, new { htmlAttributes = new { @class = "form-control" } })
					@Html.ValidationMessageFor(model => model.CustomerAddress, "", new { @class = "text-danger" })

				</div>
			</div>
			<div class="dtr-details">
				<table class="table table-responsive table-bordered">
					<tr style="background-color:palegoldenrod">

						<td>Contry </td>
						<td>state </td>
						<td>City</td>
						<td>Pin Code</td>
						<td> </td>
					</tr>
					<tr class="mycontainer" id="mainrow">
						<td>
							<div>
								@Html.DropDownListFor(m => m.CountryID, ViewBag.ContList as SelectList, "-- Select Contry --", new { @class = "form-control" })
								@Html.ValidationMessageFor(model => model.Contry, "", new { @class = "text-danger" })
							</div>
						</td>


						<td>
							<div>
								@Html.DropDownListFor(m => m.StatelistID, new SelectList(" ViewBag.stList"), "-- Select Contry --", new { @class = "form-control" })
								@Html.ValidationMessageFor(model => model.StatelistID, "", new { @class = "text-danger" })
							</div>
						</td>
					</tr>
				</table>
			</div>
		</div>
	</form>
</body>
</html>
@section Scripts{
     <script src="~/Scripts/jquery-1.10.2.min.js"></script>
	<script >
		$(document).ready(function () {
			$("#CountryID").change(function () {
				var contId = $(this).val();
				debugger
				$.ajax({
					type: "post",
					url: "/Cust/GetState?CountryID=" + contId,
					contentType: "html",
					success: function (response) {
						debugger
						$("#StatelistID").empty();
						$("#StatelistID").append(response);
					}
				})  
			})
		})
	</script>
}
Posted
Updated 31-Dec-18 7:37am
Comments
F-ES Sitecore 31-Dec-18 10:51am    
"Doesn't work" gives no-one enough information to help solve the problem. Use the debugger and network tools to see if your action is being called. One possible issue is your url is "/Cust/GetState" which will only work if your site is at the root, something we don't know. Use Url.Action to build the url param to ensure it is valid rather than hard-coding it.

http://www.rubencanton.com/blog/2013/04/requesting-to-c-mvc-net-using-jquery-ajax.html

The problem could be lots of other things too, you can't just dump a load of code and expect other people to work out the problems, learn to use the debugging tools available to at least get a better idea how far your code gets before it stops doing what you want it to do.
yogesh vaidya 31-Dec-18 12:08pm    
thanks sir,
i was tried with step debug , first he wait on @section Scripts and then ran a js scripts 3.3.1 there first he shows url- Cust/ GetState and Contry Id ,and then he went to controller in acction method GetState and then he gose to PartialView page and through the error Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: ''System.Web.Mvc.SelectListItem' does not contain a definition for 'value' ,


in the PartialView page when i open a foreach loop when i put mouse on Item it's show i singel and first record ,

1 solution

The issue in your code as posted is that your partial view is iterating through a SelectList so "item" is going to be a SelectListItem. You then access "value" and "text"

<option value="@item.value">@item.text</option>


However the proper properties are "Value" and "Text", and c# is case sensitive so you need to change your view to

<option value="@item.Value">@item.Text</option>


One of the problems with using ViewBag is that you don't know what the types are in your view so VS can't help you when you're doing things wrong. You're better ditching the viewbag and use typed models, and you can also stop using the SelectList as that is mainly used for rendering a listbox on the view, whereas you are constructing html manually in a view. If you want to stick with your SelectList then change your view like this;

@model System.Web.Mvc.SelectList
<option value="">-- Select States --</option>
@if (Model != null)
{
    foreach (var item in Model)
    {
        <option value="@item.Value">@item.Text</option>
    }
}


Now VS knows what type "item" is and lets you use intellisense and tells you if you use the wrong properties. Now change the controller;

public ActionResult GetState(int CountryID)
{
    List<State_list> states = GetStateList().Where(x => x.CountryID == CountryID).ToList();
    SelectList stList = new SelectList(states, "StatelistID", "STATE", "CountryID");

    // pass the model to the view in the second parameter
    return PartialView("DisplayState", stList);
}


You can improve this even more by getting rid of the SelectList altogether. The view;

@model List<ChaBuss.Models.State_list>
<option value="">-- Select States --</option>
@if (Model != null)
{
    foreach (var item in Model)
    {
        <option value="@item.StatelistID">@item.STATE</option>
    }
}


Controller;

public ActionResult GetState(int CountryID)
{
    List<State_list> states = GetStateList().Where(x => x.CountryID == CountryID).ToList();

    // pass the model to the view in the second parameter
    return PartialView("DisplayState", states);
}
 
Share this answer
 
v3

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