I am doing the ASP.net MVC 3 (Empty type and not the internet type) with the Database First approach...
What i need is
Step 1:
I just used the dropdown to display the various locations where the company is located. The list comes from the Organization table and Location is only one string field in this Oranization Table,
Step 2:
While the user is doing registration, the dropdown list will show the locations.. Now, user selects India, then this value (Location Name) should store in the UserLogin Table...
Now how to read the value from the dropdown and i hope you understand my question and thanks in advance
// Coming to coding part, i used like this...
// In my Signup Control i wrote like this
public ActionResult Index()
{
ViewBag.OLocation = new SelectList(dbcontext.Organization_Details, "OName", "OLocation");
return View();
}
// and in its view, i wrote like this...
//and This View is strongly typed with "UserLogin" View Model
// Previous Code Part
@Html.Dropdownlist("OLocation")
So I assume, you are getting the Location in the return view.
which means, if your create method is like below, you have location in the loginviewmodel.location, is that right? if no, please provide, your login model and logincreate method.
[HttpPost]
public ActionResult LoginCreate(LoginViewModel loginviemodel)
{
if (ModelState.IsValid)
{
db.Login.Add(loginviemodel);
db.SaveChanges();
}
return RedirectToAction("Index");
}
Don't forget to "Mark As Answer" if a post helps you.
It is about cascading dropdownlists with countries and regions, but you can take from it what you need. It shows how to bind a dropdownlist to a model and a nice setup for getting details to the dropdownlist. The full working adjusted project you can download
from
here: The project is called CascadedDdl.zip and shows two different ways of binding a dropdownlist to a model.
If you have any questions just let me know.
Hope this helps.
Regards,
Yorrick
Live life loosely coupled and separated of concerns
&
Don't forget to click "Mark As Answer" on the post that helped you.
Hi I hope your code is sufficent to add the User to db and i am sure in that...
but i am not able to assing the data which is picked from the dropdown
see here once
// Your Code
[HttpPost]
public ActionResult LoginCreate(LoginViewModel loginviemodel)
{
if (ModelState.IsValid)
{
db.Login.Add(loginviemodel);
db.SaveChanges();
}
return RedirectToAction("Index");
}
// My Code
public void addUser(User_Details_Class UDCObj)
{
// Creating Object to Store data and assingning Data
WM_DB.User_Details UDObj = new User_Details();
// Assinging
UDObj.UId = UDCObj.UId;
UDObj.UName = UDCObj.UName;
UDObj.UPassword = UDCObj.UPassword;
UDObj.ULocation = // you write your code here to catch the data which is selected from drop down
// Here it is clear that i am assigning values one by one like id,password and
// here i am failed to read the value form dropdown.
// If we catch then surely we can add it into user table by combing my code and your code...
// I hope u understand my code
//Saving Data
dbcontext.User_Details.AddObject(UDObj);
dbcontext.SaveChanges();
}
pavankumarmc...
Member
7 Points
97 Posts
Read The Data From DropDown in ASP.Net MVC3
May 12, 2012 05:39 AM|LINK
Hi all,
I am doing the ASP.net MVC 3 (Empty type and not the internet type) with the Database First approach...
What i need is
Step 1:
I just used the dropdown to display the various locations where the company is located. The list comes from the Organization table and Location is only one string field in this Oranization Table,
Step 2:
While the user is doing registration, the dropdown list will show the locations.. Now, user selects India, then this value (Location Name) should store in the UserLogin Table...
Now how to read the value from the dropdown and i hope you understand my question and thanks in advance
abdu292
Participant
1553 Points
371 Posts
Re: Read The Data From DropDown in ASP.Net MVC3
May 12, 2012 06:17 AM|LINK
I already worked on something like this. Can you tell me,
If you are using Entity frame work?
How are you populating Dropdown list?
Are you using a model binder?
Could you provide some code snippets, as to how the location is getting displayed in the login page, method to return the view to controller etc?
With best regards,
pavankumarmc...
Member
7 Points
97 Posts
Re: Read The Data From DropDown in ASP.Net MVC3
May 12, 2012 06:21 AM|LINK
ya sure i followed this link
http://geekswithblogs.net/dotNETvinz/archive/2011/06/03/asp.net-mvc-3-creating-a-simple-sign-up-form.aspx
http://geekswithblogs.net/dotNETvinz/archive/2011/12/30/asp.net-mvc-3---creating-a-simple-log-in-form.aspx
and
i used the Database first approach and
i used the Entity FrameWork..
mvc
pavankumarmc...
Member
7 Points
97 Posts
Re: Read The Data From DropDown in ASP.Net MVC3
May 12, 2012 06:22 AM|LINK
// Coming to coding part, i used like this... // In my Signup Control i wrote like this public ActionResult Index() { ViewBag.OLocation = new SelectList(dbcontext.Organization_Details, "OName", "OLocation"); return View(); } // and in its view, i wrote like this... //and This View is strongly typed with "UserLogin" View Model // Previous Code Part @Html.Dropdownlist("OLocation")abdu292
Participant
1553 Points
371 Posts
Re: Read The Data From DropDown in ASP.Net MVC3
May 12, 2012 06:37 AM|LINK
So, What value it returns for the dropdown list? Are you getting OName instead of OLocation?
You are expecting OLocation to be returned with the View?
With best regards,
pavankumarmc...
Member
7 Points
97 Posts
Re: Read The Data From DropDown in ASP.Net MVC3
May 12, 2012 06:46 AM|LINK
yes, u r correct
and
it will returning OLocation correctly and i solved upto here
and it is string type in database like India, England etc and
OName is also string and it is company name like Microsoft, Yahoo etc
now coming to point
if the user selects the India, England etc then this value have to store in db.
I am not able to write correct code in Controller and in [HttpPost] part as this MVC 3 completely new me
and in my past i worked on MS CRM and i am less than one 15 days old in this MVC Concepts..
So, I would be fine if u gudie me..
Thanks for replying
abdu292
Participant
1553 Points
371 Posts
Re: Read The Data From DropDown in ASP.Net MVC3
May 12, 2012 07:15 AM|LINK
So I assume, you are getting the Location in the return view.
which means, if your create method is like below, you have location in the loginviewmodel.location, is that right? if no, please provide, your login model and logincreate method.
[HttpPost] public ActionResult LoginCreate(LoginViewModel loginviemodel) { if (ModelState.IsValid) { db.Login.Add(loginviemodel); db.SaveChanges();return RedirectToAction("Index"); }With best regards,
Yorrick vd V...
Participant
1674 Points
301 Posts
Re: Read The Data From DropDown in ASP.Net MVC3
May 12, 2012 07:20 AM|LINK
Hi,
Take a look at the follwoing post: http://forums.asp.net/t/1748239.aspx/1?Cascading+drop+downs+from+database+table+rows
It is about cascading dropdownlists with countries and regions, but you can take from it what you need. It shows how to bind a dropdownlist to a model and a nice setup for getting details to the dropdownlist. The full working adjusted project you can download from here: The project is called CascadedDdl.zip and shows two different ways of binding a dropdownlist to a model.
If you have any questions just let me know.
Hope this helps.
Regards,
Yorrick
&
Don't forget to click "Mark As Answer" on the post that helped you.
pavankumarmc...
Member
7 Points
97 Posts
Re: Read The Data From DropDown in ASP.Net MVC3
May 12, 2012 07:33 AM|LINK
Hi I hope your code is sufficent to add the User to db and i am sure in that...
but i am not able to assing the data which is picked from the dropdown
see here once
// Your Code [HttpPost] public ActionResult LoginCreate(LoginViewModel loginviemodel) { if (ModelState.IsValid) { db.Login.Add(loginviemodel); db.SaveChanges(); } return RedirectToAction("Index"); } // My Code public void addUser(User_Details_Class UDCObj) { // Creating Object to Store data and assingning Data WM_DB.User_Details UDObj = new User_Details(); // Assinging UDObj.UId = UDCObj.UId; UDObj.UName = UDCObj.UName; UDObj.UPassword = UDCObj.UPassword; UDObj.ULocation = // you write your code here to catch the data which is selected from drop down // Here it is clear that i am assigning values one by one like id,password and // here i am failed to read the value form dropdown. // If we catch then surely we can add it into user table by combing my code and your code... // I hope u understand my code //Saving Data dbcontext.User_Details.AddObject(UDObj); dbcontext.SaveChanges(); }abdu292
Participant
1553 Points
371 Posts
Re: Read The Data From DropDown in ASP.Net MVC3
May 12, 2012 08:50 AM|LINK
Oh, it seems, pretty straight forward. May I know what is the error are you getting? Provide your view code as well.
With best regards,