MVC DropDownListFor getting 2 names - asp.net-mvc

Hi I am developing a banking system for a school assignment. Can you pass 2 Names in a dropdownlist ex:
This is the model
public TransferModel(int bankId, int userid)
{
myAccount = new Service1Client().getAccountByID(bankId);
AccountList = new SelectList(new Service1Client().RetrieveAllAccountsByAccountId(userid), "Id", "Name");
}
I am setting Id, Name to the Select List
View:
<div class="editor-field">
Withdraw From: <%: Html.DropDownListFor(model => model.myAccount.Id, Model.AccountList)%>
</div>
the Select list will only display the name of the accounts. is their by any chance to display the name and the balance in the drop down list.
Thanks

Something like the following would be quite a tidy way of handling this:
public class AccountModel
{
public string Id { get; set; }
public string Name { get; set; }
public Decimal Balance { get; set; }
public string AccountSummary
{
get
{
return String.Format("{0} ({1:C})", Name, Balance);
}
}
}
public class TransferModel
{
public IList<AccountModel> Accounts { get; set; }
public string SelectedAccountId { get; set; }
/* Whatever other properties your view needs */
}
Type your view to TransferModel and do:
<%: Html.DropDownListFor(model => model.SelectedAccountId,
new SelectList { Model.Accounts, "Id", "AccountSummary" )%>

Related

How do I show validation error when users submit the form without selecting anything from the drop-down list in ASP.NET MVC?

In my ASP.NET MVC application, I have a form with drop-down list. If the user did not select a category and submitted the form, I will get exception. So How do I send validation error from the controller to the view? because I don't want to write JavaScript.
This is the drop-down list:
#Html.DropDownListFor(m => m.Article.CategoryId, new SelectList(Model.Categories, "Id", "Name"), "Select Category", new { #class = "form-control" })
I know this way is working but I need that label (Select Category)
#Html.DropDownListFor(m => m.Article.CategoryId, new SelectList(Model.Categories, "Id", "Name"), null, new { #class = "form-control" })
All and New controllers:
[HttpPost]
public ActionResult New(Article article)
{
if(ModelState.IsValid)
{
string FullName = HttpContext.GetOwinContext()
.GetUserManager<ApplicationUserManager>()
.FindById(User.Identity.GetUserId()).FullName;
article.AuthorName = FullName;
article.UserId = User.Identity.GetUserId();
db.Aricles.Add(article);
db.SaveChanges();
return RedirectToAction("All");
}
}
public ActionResult All()
{
var Articles = db.Aricles.ToList();
return View(Articles);
}
My model:
public class Article
{
public int Id { get; set; }
[Required]
public string Title { get; set; }
[Required]
public string Body { get; set; }
public string UserId { get; set; }
public string AuthorName { get; set; }
public ApplicationUser User { get; set; }
public Category Category { get; set; }
public int CategoryId { get; set; }
}
I tried to use [Required] attribute but I don't know why it does not work.
This is a screenshot of the exception click
I found the issue after I thought about the comment written by Mannan Bahelim . I don't get validation error because the model in the view was ArticleCategoryViewModel while it should be Article model.

Populate DropDownList in ASP.NET MVC from database table using Entity Framework 6 and ViewModel

I have been scratching my head for a whole night on an issue I can do quickly using ajax/jquery and stored procedures. I want to
1) Populate a drop down list from values obtained from a database table using Entity Framework and view model. I DO NOT WANT TO USE VIEWBAG OR VIEWDATA. Any help appreciated.
2) How can I generate a Create View using the View Model with the all the default fields ? The scaffholding works on a model but not on a view model ?
MY MODELS
public class Employee
{
public int EmployeeID { get; set; }
public string Name { get; set; }
public string Gender { get; set; }
public string City { get; set; }
public string Level { get; set; }
public int DepartmentId { get; set; }
}
public class Grade
{
public int ID { get; set; }
public string Level { get; set; }
}
View Model
public class GradeSelectListViewModel
{
public Employee Employee { get; set; }
public IEnumerable<SelectListItem> Grades { get; set; }
public GradeSelectListViewModel(Employee employee, IEnumerable grades)
{
Employee = employee;
Grades = new SelectList(grades, "Grade", "Name", employee.Level);
}
}
MY CONTEXT CLASS
public class EmployeeContext : DbContext
{
public DbSet<Employee> Employees { get; set; }
public DbSet<Department> Departments { get; set; }
public DbSet<Grade> Grades { get; set; }
}
MY CONTROLLER
public ActionResult Edit (int? id)
{
using (var db = new EmployeeContext())
{
var model = new GradeSelectListViewModel(db.Employees.Find(id), db.Grades);
//model.Employee = db.Employees.Single(x => x.EmployeeID == id);
model.Grades = db.Grades.ToList().Select(x => new SelectListItem
{
Value = x.ID.ToString(),
Text = x.Level
});
return View(model);
}
}
MY RAZOR PAGE CSHTML
#model MVCDemo.ViewModels.GradeSelectListViewModel
....
#using (Html.BeginForm())
{
#Html.AntiForgeryToken()
....
#Html.DropDownListFor(x => Model.Employee.Level,
new SelectList(Model.Grades, "ID", "Level"),
"Select Level")
....
<input type="submit" value="Create" class="btn btn-default" />
}
The main issue is that in the view you have new SelectList(Model.Grades, "ID", "Level") but Grades is IEnumerable<SelectListItem> and SelectListItem does not contain properties named ID and Level.
However there are a a few other issues with your code. First a view model should not contain a data model, and instead your view model should be
public class GradeSelectListViewModel
{
public int? ID { get; set; } // make this ID so you do not need an input for it
public string Name { get; set; }
.... // other properties of Employee that your editing
[Required(ErrorMessage = "..")]
public int? Level { get; set; } // make value types nullable to protect against under-posting attacks
public IEnumerable<SelectListItem> Grades { get; set; }
}
and add display and validation attributes as required. Note that I deleted the constructor (you don't seem to be using it, but if you did, then you also need to include a parameter-less constructor, otherwise an exception will be thrown when submitting to the POST method. I also assume that Level should be typeof int since you binding to the int ID property of Grade.
The the code in your GET method should be
Employee employee = db.Employees.Find(id);
var model = new GradeSelectListViewModel()
{
ID = employee.EmployeeID,
Name = employee.Name,
Level = employee.Level, // convert to int?
....
Grades = db.Grades.Select(x => new SelectListItem
{
Value = x.ID.ToString(),
Text = x.Level
})
};
return View(model);
and in the view
#Html.DropDownListFor(x => Model.Level, Model.Grades, "Select Level")
Note also that in the POST method, your need to reassign the SelectList if you return the view because ModelState is invalid.
You can use the following approach that populates three DropDownListFor at the same View:
ViewModel:
public class GroupViewModel
{
public IEnumerable<SelectListItem> Schedules { get; set; }
public int ScheduleId { get; set; }
public IEnumerable<SelectListItem> Labs { get; set; }
public int LabId { get; set; }
public IEnumerable<SelectListItem> Terms { get; set; }
public int TermId { get; set; }
}
Controller:
public ActionResult Create()
{
//Populate DropDownList binding values
var model = new GroupViewModel
{
//Preselect the Lab with id 2
//LabId = 2,
Labs = repository.Labs.Select(c => new SelectListItem
{
Value = c.Id.ToString(),
Text = c.Name
}),
Terms = repository.Terms.Select(c => new SelectListItem
{
Value = c.Id.ToString(),
Text = c.Name
}),
Schedules = repository.Schedules.Select(c => new SelectListItem
{
Value = c.Id.ToString(),
Text = c.Name
})
};
return View("Create", model);
}
View:
#Html.DropDownListFor(m => m.LabId, new SelectList(Model.Labs, "Value", "Text"),
"Select", new { #class = "selectpicker" })
#Html.DropDownListFor(m => m.ScheduleId, new SelectList(Model.Schedules, "Value", "Text"),
"Select", new { #class = "selectpicker" })
#Html.DropDownListFor(m => m.TermId, new SelectList(Model.Terms, "Value", "Text"),
"Select", new { #class = "selectpicker" })

MVC Multiple tables one model

I am having difficulty with my understanding of MVC coming from an aspx world.
I have a Model called CustomerGarment. This has a Order and a Customer along with a few garments.
public class CustomerGarment
{
public int CustomerGarmentId { get; set; }
public virtual Customer Customer { get; set; }
public virtual Order Order { get; set; }
public virtual GarmentJacket GarmentJacket { get; set; }
public virtual GarmentShirt GarmentShirt { get; set; }
}
I have a method for get and post. When the page loads, it creates a new CustomerGarment instance and querys the database to fill the Customer and Order variables. I then use the viewbag to show on the screen a list of GarmentJackets and GarmentShirts
The page then views and using the view I can access the model perfectly. Drop downs load with the viewbag contents and I can access all Customer and Order variables using the model I have passed.
The problem I then face is when I use the HttpPost. The model is not passed back with the information I passed to it.
public ActionResult AddGarments(int orderId, int customerId)
{
CustomerGarment cg = new CustomerGarment();
cg.Order = (from a in db.Orders where a.OrderId == orderId select a).FirstOrDefault();
cg.Customer = (from a in db.Customers where a.CustomerId == customerId select a).FirstOrDefault();
var jackets = from a in db.GarmentJackets orderby a.Type, a.SleeveLengthInches, a.ChestSizeInches select a;
var shirts= from a in db.GarmentKilts orderby a.PrimarySize, a.DropLength select a;
ViewBag.GarmentJacket = new SelectList(jackets, "GarmentJacketId", "GarmentJacketId");
ViewBag.GarmentShirt = new SelectList(shirts, "GarmentShirtId", "GarmentShirtId");
return View(cg);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult AddGarments(CustomerGarment cg)
{
// Here, I do not have the customer info for example
db.CustomerGarments.Add(cg);
db.SaveChanges();
return RedirectToAction("Index");
return View(cg);
}
This is a bit of my view
#Html.HiddenFor(model => model.Order.OrderId)
#Html.HiddenFor(model => model.Order.CustomerId)
<div class="display-field">
#Html.DisplayFor(model => model.Customer.Name)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.GarmentJacket, "Jacket")
</div>
<div class="editor-field">
#Html.DropDownListFor(m => m.GarmentJacket, (SelectList)ViewBag.GarmentJacket, new {style="width:312px;height:30px;margin-top:2px;margin-bottom:5px"})
</div>
EDIT
My Garment Jacket Model
public class GarmentJacket : Garment
{
public int GarmentJacketId { get; set; }
[Required]
public string Type { get; set; }
[Required]
[Display(Name = "Chest Size")]
public int ChestSizeInches { get; set; }
[Required]
[Display(Name = "Sleeve Length")]
public int SleeveLengthInches { get; set; }
}
public class Garment
{
[DataType(DataType.Date)]
public DateTime? DateRetired { get; set; }
[Required]
public string Barcode { get; set; }
[Required]
public bool Adults { get; set; }
}
In your CustomerGarment class, you should have:
public class CustomerGarment
{
public int CustomerGarmentId { get; set; }
public int CustomerId { get; set; }
public int OrderId { get; set; }
public int GarmentJacketId { get; set; }
public int GarmentShirtId { get; set; }
public virtual Customer Customer { get; set; }
public virtual Order Order { get; set; }
public virtual GarmentJacket GarmentJacket { get; set; }
public virtual GarmentShirt GarmentShirt { get; set; }
}
And, then, in your View, your DropDownList will look like:
#Html.DropDownListFor(m => m.GarmentJacketId, (SelectList)ViewBag.GarmentJacket, new {style="width:312px;height:30px;margin-top:2px;margin-bottom:5px"})
Your DropDownList only posts one value, which is the GarmentJacketId. You can't bind that Id to the whole GarmentJacket class.
By the way, you also need to replace your hidden inputs with these:
#Html.HiddenFor(model => model.OrderId)
#Html.HiddenFor(model => model.CustomerId)
I think I know your problem. As you suggested in you comment above you need to post everything you want retained in the view. This is one of the differences beteween webforms and MVC, webforms has viewstate that could contain information that you don't explicitly add to the view and post back, giving the impression of state. In MVC you have to add it to the view.
On the other hand you don't need to pass in more information than you need either. You pass inn the customerId as a hidden field. On post method you get the customer from the db using the Id, then you add the order to the customer.
I have some questions about your design, but given that a customer holds a collection of Orders, you could do something like this:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult AddGarments(CustomerGarment cg)
{
// Get the customer from the database
var customer = db.Customers.Find(c=>c.id==cb.Customer.Id)
var order = new Order();
//Create your order here using information from CustomerGarment model
//If the model already holds a valid Order object then just add it.
//i.e. you could get a Garment object from the DB using the GarmentId from
//the ViewModel if you really need more than just the Id to create the order
customer.Orders.Add(order);
db.SaveChanges();
return RedirectToAction("Index");
}

Deleting relational Non-Content data from Orchard CMS

I have declared some Non-Content data in an Orchard CMS by defining the records and schema like this:
public class CountyRecord
{
public virtual int Id { get; set; }
public virtual string CountyName { get; set; }
public virtual CountryRecord CountryRecord { get; set; }
}
public class CountryRecord
{
public CountryRecord()
{
CountyRecords = new List<CountyRecord>();
}
public virtual int Id { get; set; }
public virtual string CountryName { get; set; }
public virtual IList<CountyRecord> CountyRecords { get; set; }
}
public class Migrations: DataMigrationImpl
{
public int Create()
{
//COUNTIES
SchemaBuilder.CreateTable(typeof(CountyRecord).Name, table => table
.Column<int>("Id", col => col
.PrimaryKey()
.Identity())
.Column<string>("CountyName")
.Column<int>("CountryRecord_Id"));
//COUNTRIES
SchemaBuilder.CreateTable(typeof(CountryRecord).Name, table => table
.Column<int>("Id", col => col
.PrimaryKey()
.Identity())
.Column<string>("CountryName"));
}
}
I then have two controllers handling the admin pages for these two entities. In the country controller I have the following actions:
//DELETE
[HttpGet, Admin]
public ActionResult Delete(int countryId)
{
var country = CountryRepo.Get(countryId);
if (country == null)
{
return new HttpNotFoundResult("Couldn't find the country with ID " + countryId.ToString());
}
return View(country);
}
[HttpPost, Admin, ActionName("Delete")]
public ActionResult DeletePOST(CountryRecord country)
{
foreach (CountyRecord county in CountyRepo.Fetch(c=>c.CountryRecord.Id==country.Id))
{
CountyRepo.Delete(county);
}
CountryRepo.Delete(country);
OrchardServices.Notifier.Add(NotifyType.Information, T("Country '{0}' deleted successfully", country.CountryName));
return RedirectToAction("Index");
}
And this is the view that goes with that:
#model Addresses.Models.CountryRecord
<div class="manage">
#using (Html.BeginFormAntiForgeryPost("Delete"))
{
<h2>Are you sure you want to delete this country and ALL its counties?</h2>
#Html.HiddenFor(m => m.Id);
#Html.HiddenFor(m => m.CountryName);
#Html.ActionLink(T("Cancel").Text, "Index", "CountriesAdmin", new { AreaRegistration = "Addresses" }, new { style = "float:right; padding:4px 15px;" })
<button class="button primaryAction" style="float:right;">#T("Confirm")</button>
}
</div>
However, here's the issue, when I delete a country that still has counties assigned to it, it throws the following error:
a different object with the same identifier value was already associated with the session
Can anyone please help?
Thanks.
It's because your DeletePOST() parameter is a CountryRecord. Orchard records are all proxied by the NHibernate framework and MVC's ModelBinder can't properly create them for you.
What you need to do instead is like what you do in the non-POST method: accept just the integer ID of the CountryRecord, fetch the record fresh from the repository, then delete it.

LIstbox in MVC 2

I am having a Employee Table. From that i want to load the Employee Names in a List Box. I dont know from where i should start. Kindly guide me.
As always start by defining the view model that will represent your data:
public class Employee
{
public string Id { get; set; }
public string Name { get; set; }
}
public class MyViewModel
{
public string SelectedEmployeeId { get; set; }
public IEnumerable<Employee> Employees { get; set; }
}
Then the controller which will manipulate the model:
public class HomeController : Controller
{
public ActionResult Index()
{
var model = new MyViewModel
{
// TODO: Instead of hardcoding fetch from your repository
Employees = Enumerable.Range(1, 5).Select(i => new Employee
{
Id = i.ToString(),
Name = "employee " + i
})
};
return View(model);
}
}
And finally generate a dropdown list in the view:
<%: Html.DropDownListFor(
x => x.SelectedEmployeeId,
new SelectList(Model.Employees, "Id", "Name")
) %>
If you want to allow multiple selections a.k.a ListBox a few changes are necessary. First you need an array of employee ids in your model:
public class MyViewModel
{
public string[] SelectedEmployeeIds { get; set; }
public IEnumerable<Employee> Employees { get; set; }
}
And then use the ListBoxFor helper in the view:
<%: Html.ListBoxFor(
x => x.SelectedEmployeeIds,
new SelectList(Model.Employees, "Id", "Name")
) %>
you could also try my AjaxDropdown helper and populate your listbox via jquery Ajax (you don't have to know anything about jquery)
http://awesome.codeplex.com/
there is a live demo that you can try and download

Resources