I am newbie to work on ASP.NET MVC 3 pages. I would like to design a page which includes two dropdownlists that populate the school list and location in the database. Based on the user select school item and location item that will call store procedure in
the backend SQL Server for displaying the details of student records. I am not sure how to do it in ASP.NET MVC for handling the session state to use in the query filter. Please help me to make it happen. Thanks for your inputs.
Thanks for your reply but I could not get what you said. Please show the details how to code above scenario. Here what I need to do as following actions:
For example on my simple database; School table has unique schoolId for primary key, schoolName columns, Location table has unique locationId for primary key, city, state and Student table has studentId for primary key, studentName, studentGrade, studentId
and locationId for foreign keys.
1. Need to populate two dropdownlist data for school and location from the database.
2. Need a form to allow the user to select items from combo boxes, then based on select the values of dropdownlist items to do filter query to display the list of studentName and Grade that call a stored procedure sp_studentlist from backend SQLServer using
Razor engine.
Learning start on ASP.NET MVC are difficult because there are a lot of resources, so your hints are much appreciated to narrow down on specific discussion. Thanks in advance.
Thanks for your reply but I could not get what you said. Please show the details how to code above scenario. Here what I need to do as following actions:
For example on my simple database; School table has unique schoolId for primary key, schoolName columns, Location table has unique locationId for primary key, city, state and Student table has studentId for primary key, studentName, studentGrade, studentId
and locationId for foreign keys.
1. Need to populate two dropdownlist data for school and location from the database.
2. Need a form to allow the user to select items from combo boxes, then based on select the values of dropdownlist items to do filter query to display the list of studentName and Grade that call a stored procedure sp_studentlist from backend SQLServer using
Razor engine.
Learning start on ASP.NET MVC are difficult because there are a lot of resources, so your hints are much appreciated to narrow down on specific discussion. Thanks in advance.
What you need to do is not easily done with MVC.
Essentially what you want to do is present two combo boxes with the second combo boxes content filteterd by the first combo box selection.
To accomplish this you'll need a bit of JavaScript and will need to use Unobtrusive AJAX for the second combo box.
The JavaScript that you'll need will post back to your controller with the first combo box selected value. I'm not a JavaScript expert, so I'll have to leave that task as an exercize for the reader.
You'll need to create a partial view with the second combo box on it and create a method on your controller that delivers that partial view.
Your use case is what AJAX was desinged for. If you need help with JavaScript search the this fourm because I saw what you are looking for recently.
I built the same scenario in ASP.NET Webform using session or view state easily. Recently, they would like to convert to ASP.NET MVC 3, so I posted it for helping. I do believe ASP.NET MVC 3 can do it even though it's not easy as you state. Let simplify
on above issue, just populate data for combo box only, the second combo box is not related to first one. Can we do it by combining the ASP.NET MVC 3 , jquery or whatever javascript? How do can I solve above scenario using Model View Control? Any expertise
in ASP.NET MVC Web Application coding to help me out? Thanks.
Here are my dropdowlist that binding data from entity framework as following:
In Models folder, I added the DataRepository.cs
public classDataRepository
{
//Create an instance of DDLEntities
publicDDLEntities
ddlEntity = newDDLEntities();
//Fetching data from table
publicList<SelectListItem>
GetStudentName()
{
var StudentName = (from studentName
in
ddlEntity.StudentRecords
selectnewSelectListItem
{
Text = studentName .NAME,
Value = studentName .NAME
});
return StudentName.ToList();
}
I am not sure how to use your code to apply on my problem. If you can help me an example for ASP.NET MVC 3 that have one dropdownlist that populate items from entity framework and based on select values to filter the simple query to display the result on
Razor view. I am lost totally on what you said Unobrusive AJAX. Can you attached small example, so I can download and try it. Thanks in advance.
Re-Read my intial post and follow those steps exactly to enable Unobtrusive AJAX for you project.
Once you've done that I may be able to help you further, but you MUST do that first.
With MVC 3, the DATA for the dropdowns must be passed to the VIEW (or Partial View) from the controller.
My MVC 3 book apparently doesn't cover @Html.DropDown at all, so I'm probably not the right person to help you any further with that part of your problem area.
I can populate the data for two comboboxes: School and Location from Entity Framework tables. How do I can pass the selected values from dropdownlist when click on Submit button to stored procedure in the backend SQL server and display the student record
result in the front end using the Razor. At this time, I do not need the ajax yet. Any small attached sample code are much apreciated. Thanks.
public class SchoolViewModel
{
public int schoolID { get; set; }
public List<School> Schools { get; set; }
}
And a School model class like this:
public class School
{
public int ID { get; set; }
public string Name { get; set; }
}
In your view, you would create your dropdown as follows:
@Html.DropDownListFor(
x => x.SchoolID,
new SelectList(Model.Schools, "ID", "Name"))
Now, in your controller, the selected value is automatically mapped onto the SchoolID property of your viewmodel:
[HttpPost]
public ActionResult Create(SchoolViewModel viewModel)
{
//your selected value is automatically mapped to viewModel.SchoolID by the framework's modelbinder
//now you can do something with this ID
}
avt2k7
Member
290 Points
210 Posts
MVC 3 Filter Design!
Dec 22, 2012 02:39 AM|LINK
Hi all,
I am newbie to work on ASP.NET MVC 3 pages. I would like to design a page which includes two dropdownlists that populate the school list and location in the database. Based on the user select school item and location item that will call store procedure in the backend SQL Server for displaying the details of student records. I am not sure how to do it in ASP.NET MVC for handling the session state to use in the query filter. Please help me to make it happen. Thanks for your inputs.
ignatandrei
All-Star
135073 Points
21662 Posts
Moderator
MVP
Re: MVC 3 Filter Design!
Dec 22, 2012 02:46 AM|LINK
IN Session_Start, put your value
Use in query filter.
avt2k7
Member
290 Points
210 Posts
Re: MVC 3 Filter Design!
Dec 22, 2012 12:14 PM|LINK
Hi,
Thanks for your reply but I could not get what you said. Please show the details how to code above scenario. Here what I need to do as following actions:
For example on my simple database; School table has unique schoolId for primary key, schoolName columns, Location table has unique locationId for primary key, city, state and Student table has studentId for primary key, studentName, studentGrade, studentId and locationId for foreign keys.
1. Need to populate two dropdownlist data for school and location from the database.
2. Need a form to allow the user to select items from combo boxes, then based on select the values of dropdownlist items to do filter query to display the list of studentName and Grade that call a stored procedure sp_studentlist from backend SQLServer using Razor engine.
Learning start on ASP.NET MVC are difficult because there are a lot of resources, so your hints are much appreciated to narrow down on specific discussion. Thanks in advance.
eric2820
Contributor
2777 Points
1161 Posts
Re: MVC 3 Filter Design!
Dec 22, 2012 12:35 PM|LINK
What you need to do is not easily done with MVC.
Essentially what you want to do is present two combo boxes with the second combo boxes content filteterd by the first combo box selection.
To accomplish this you'll need a bit of JavaScript and will need to use Unobtrusive AJAX for the second combo box.
The JavaScript that you'll need will post back to your controller with the first combo box selected value. I'm not a JavaScript expert, so I'll have to leave that task as an exercize for the reader.
You'll need to create a partial view with the second combo box on it and create a method on your controller that delivers that partial view.
Your use case is what AJAX was desinged for. If you need help with JavaScript search the this fourm because I saw what you are looking for recently.
http://www.my-msi.net/Admin
blog
If a post helps you, please mark it as Ansered, thank-you.
avt2k7
Member
290 Points
210 Posts
Re: MVC 3 Filter Design!
Dec 22, 2012 01:22 PM|LINK
Hi,
I built the same scenario in ASP.NET Webform using session or view state easily. Recently, they would like to convert to ASP.NET MVC 3, so I posted it for helping. I do believe ASP.NET MVC 3 can do it even though it's not easy as you state. Let simplify on above issue, just populate data for combo box only, the second combo box is not related to first one. Can we do it by combining the ASP.NET MVC 3 , jquery or whatever javascript? How do can I solve above scenario using Model View Control? Any expertise in ASP.NET MVC Web Application coding to help me out? Thanks.
eric2820
Contributor
2777 Points
1161 Posts
Re: MVC 3 Filter Design!
Dec 22, 2012 01:53 PM|LINK
AjaxOptions ajaxOpts = new AjaxOptions
{ LoadingElementId = "YourTargetDivId",
HttpMethod = "Post",
UpdateTargetId = "YourTargetId",
LoadingElementDuration=100
};
The above goes below the Layout variable at the top of your View.
Next you need to add a <div id="YourTargetDivId"> and within that <div> you'll need to add some other control with id ="YourTargetId"
@Ajax.ActionLink( selected value from first combo box, "YourMethodName", ajaxOpts )
The above line calls AJAX to reload the selected element of the page without needing to refresh the entire page.
Next in your _Layout.cshtml you'll need to add this:
<script src="@Url.Content( "~/Scripts/jquery-1.5.1.js" )" type="text/ecmascript"></script>
<script src="@Url.Content( "~/Scripts/jquery-unobtrusive-ajax.js" )" type="text/javascript"></script>
<script src="@Url.Content( "~/Scripts/jquery-validate.js" )" type="text/javascript"></script>
<script src="@Url.Content( "~/Scripts/jquery-validate.unobtrusive.js" )" type="text/javascript"></script>
These scripts need to be loading in the order shown in order for things to work correctly.
Pretty simple, right. That's why it's called Unobrusive AJAX :-)
http://www.my-msi.net/Admin
blog
If a post helps you, please mark it as Ansered, thank-you.
avt2k7
Member
290 Points
210 Posts
Re: MVC 3 Filter Design!
Dec 26, 2012 02:03 PM|LINK
Hi Eric,
Here are my dropdowlist that binding data from entity framework as following:
In Models folder, I added the DataRepository.cs
public class DataRepository
{ //Create an instance of DDLEntities
public DDLEntities ddlEntity = new DDLEntities();
//Fetching data from table
public List<SelectListItem> GetStudentName()
{
var StudentName = (from studentName in ddlEntity.StudentRecords
select new SelectListItem
{
Text = studentName .NAME,
Value = studentName .NAME
});
return StudentName.ToList();
}
I am not sure how to use your code to apply on my problem. If you can help me an example for ASP.NET MVC 3 that have one dropdownlist that populate items from entity framework and based on select values to filter the simple query to display the result on Razor view. I am lost totally on what you said Unobrusive AJAX. Can you attached small example, so I can download and try it. Thanks in advance.
eric2820
Contributor
2777 Points
1161 Posts
Re: MVC 3 Filter Design!
Dec 26, 2012 02:22 PM|LINK
Re-Read my intial post and follow those steps exactly to enable Unobtrusive AJAX for you project.
Once you've done that I may be able to help you further, but you MUST do that first.
With MVC 3, the DATA for the dropdowns must be passed to the VIEW (or Partial View) from the controller.
My MVC 3 book apparently doesn't cover @Html.DropDown at all, so I'm probably not the right person to help you any further with that part of your problem area.
http://www.my-msi.net/Admin
blog
If a post helps you, please mark it as Ansered, thank-you.
avt2k7
Member
290 Points
210 Posts
Re: MVC 3 Filter Design!
Dec 28, 2012 04:33 PM|LINK
Hi,
I can populate the data for two comboboxes: School and Location from Entity Framework tables. How do I can pass the selected values from dropdownlist when click on Submit button to stored procedure in the backend SQL server and display the student record result in the front end using the Razor. At this time, I do not need the ajax yet. Any small attached sample code are much apreciated. Thanks.
mgasparel
Member
298 Points
65 Posts
Re: MVC 3 Filter Design!
Dec 28, 2012 06:51 PM|LINK
Assume you have the following ViewModel:
public class SchoolViewModel { public int schoolID { get; set; } public List<School> Schools { get; set; } }And a School model class like this:
public class School { public int ID { get; set; } public string Name { get; set; } }In your view, you would create your dropdown as follows:
@Html.DropDownListFor( x => x.SchoolID, new SelectList(Model.Schools, "ID", "Name"))Now, in your controller, the selected value is automatically mapped onto the SchoolID property of your viewmodel:
[HttpPost] public ActionResult Create(SchoolViewModel viewModel) { //your selected value is automatically mapped to viewModel.SchoolID by the framework's modelbinder //now you can do something with this ID }