how to create the dropdown and validations in registe page using mvc

Posted by Kishore22 under ASP.NET MVC on 9/23/2013 | Points: 10 | Views : 1552 | Status : [Member] | Replies : 4
how to create the dropdown and validations in registe page using mvc




Responses

Posted by: Bandi on: 9/23/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
refer these links
http://www.dotnet-tricks.com/Tutorial/mvc/H1VF301212-MVC4-Registration-form-with-Client-and-Server-Side-Validation.html
http://stackoverflow.com/questions/4729440/validate-a-dropdownlist-in-asp-net-mvc
http://www.c-sharpcorner.com/UploadFile/esubal/mvc-populate-dropdown/


Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Kishore22, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/23/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
refer this link for MVC app with dropdownlist
http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/advanced-entity-framework-scenarios-for-an-mvc-web-application

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Kishore22, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/24/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
In our application we want to add new movies, and select directors from a drop-down list. The classes are structured as follows.
public class Movie
{
public int Id { get; set; }
public string Title { get; set; }
public virtual Director Director { get; set; }
public virtual int DirectorId { get; set; }
}
public class Director
{
public int Id { get; set; }
public string Name { get; set; }
}


HTML.DROPDOWNLIST VS HTML.DROPDOWNLISTFOR
To add the drop-down markup to your .cshtml page you could of course simply write out a select element by hand, but then you lose out on validation. MVC provides HTML helpers for generating common HTML elements. For an HTML select element you have two choices: Html.DropDownList and Html.DropDownListFor. The difference is the way they reference the name attribute of the resulting HTML element. DropDownList takes as it’s first argument a string that will be turned into the form field. So the call
@Html.DropDownList("director", directorList)
//assume directorList is an IEnumerable of SelectListItem to create options from
Will result in an html element that looks like this.

<select id="director" name="director">...</select>
The problem with this approach is if you change the name of the property on your model from ‘director’ to ‘auteur’ you won’t get compile time checking and your form will no longer work with model binding. Html.DropDownListFor was introduced in MVC 2 and allows binding to strongly typed views. The first argument should be a lambda function that returns the model property you want the control to bind to. So in our case if the view-model includes a property DirectorId we can create a drop-down list with the code
@Html.DropDownListFor(viewModel => viewModel.DirectorId, directorList)

Which generates the following html
<select id="DirectorId" name="DirectorId">
...
</select>

Now if we change the name of the Director property our build will break because the lambda expression will be invalid.Note that we’re using DirectorId instead of a Director object because we likely want to store the id in a foreign key.

POPULATING DROP-DOWN LIST OPTIONS
To populate a drop-down we need to pass the HTML helper an IEnumerable. This is easily created by making a SelectList object in your controller, and passing it in via a view-model.
public ActionResult Index()
{
var directors = new Collection
{
new Director {Id = 1, Name = "David O. Russell"},
new Director {Id = 2, Name = "Steven Spielberg"},
new Director {Id = 3, Name = "Ben Affleck"}
};
var selectList = new SelectList(directors, "Id", "Name");
var vm = new ViewModel {DirectorSelectList = selectList};
return View(vm);
}


SETTING A DEFAULT VALUE OF THE DROP-DOWN
var selectList = new SelectList(directors, "Id", "Name", new {id = 3});


To set the DirectorId property of the viewmodel to the value I want defaulted as follows.
var vm = new ViewModel {DirectorSelectList = selectList, DirectorId = 3};

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Kishore22, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/24/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Small example

model
public class OrganizationDTO

{
public int orgID { get; set; }
public string orgName { get; set; }
}


Controller:
public class Default1Controller : Controller

{
//
// GET: /Default1/

public ActionResult Index()
{
IList<OrganizationDTO> list = new List<OrganizationDTO>();
for (int i = 0; i < 10; i++)
{
list.Add(new OrganizationDTO { orgID = i, orgName = "Org " + i });
}

return View(list);
}

}


view:
<%= Html.DropDownListFor(m => m.First().orgID, new SelectList(Model.AsEnumerable(), "orgId","orgName")) %>


or

<%= Html.DropDownList("SomeName", new SelectList(Model, "orgID", "orgName"), "Please select Organization") %>


Reference:
http://www.c-sharpcorner.com/UploadFile/esubal/mvc-populate-dro pdown/[/LINK]

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Kishore22, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response