Creating a checkbox array in .Net MVC - asp.net-mvc-5

I have an application with a need to select all of the states (US) where a service is available. For convenience, I was trying to create the list from an enum "State", displayu array as checkboxes and save selected to database. Can't figure out the best way to create this.
Here is the class:
public class Offering
{
public int OfferingID { get; set; }
public string Name { get; set; }
public IList<State> States { get; set; }
}
public enum State
{
AL, AK, AZ, CA, CO, CT, DC, DE, FL
}
New to MVC so any help is greatly appreciated.

The simplest way without having to write your own model binders would be something like this:
<table>
<thead>
<tr>
<td></td>
<td>State</td>
</tr>
</thead>
<tbody>
#foreach (State state in Enum.GetValues(typeof(State)))
{
<tr>
<td>#Html.CheckBoxFor(model => model.States[state.ToString()].IsStateSelected)</td>
<td>#state.ToString()</td>
</tr>
}
</tbody>
Using this approach, you would need to make a new class, StateViewModel:
class StateViewModel
{
public bool IsStateSelected { get; set; }
}
and an additional property in your Offering model,
public Dictionary<string, bool> States { get; set; }

Related

Using Data Annotiations in Razor Pages while cycling through class properties

I am fairly new to Razor Pages and even .Net Core so please bear with me.
I am using Dapper to fill a class with data from an SQL Database which is working great. I can't change the long existing field names. So when I try to get headlines in a table by cycling through the properties and displaying the names in the headlines, I end up with the fairly cryptic field names. I try to add Data Annotiations to the class properties, but those do not seem to catch on to the names of the properties.
My class with annotiations:
namespace HelloRazorPage.Models
{
[BindProperties]
public class Apotheke
{
[Display(Name ="KdNr")]
public string KUNDENNUMM { get; set; }
public string BGANr { get; set; }
[Display(Name = "Name")]
public string NAMA { get; set; }
[Display(Name = "Straße")]
public string STRASSE{ get; set; }
[Display(Name = "Haus Nr.")]
public string HAUSNR { get; set; }
public string PLZ { get; set; }
[Display(Name = "Ort")]
public string ORT { get; set; }
public List<Vertraege> Vertraege { get; set; }
public bool IsNull { get; set; }
}
}
And cycling through my properties:
<table class="tableScroll">
#{var apo = (Models.Apotheke)ViewData["Apo"]; }
#if (apo != null)
{
<thead>
<tr>
#foreach(var prop in apo.GetType().GetProperties())
{<th> #prop.Name </th>}
</tr>
</thead>
This works like it's intendet to. It just does not show the annotiatons.
Tried to add data annotiatons to my class and want them to show up in the name property of [object].apo.GetType().GetProperties()

Issue with using a list to populate a table in an MVC 5 View

I have the following code in my controller:
public ActionResult Index(int Id)
{
Landbase _db = new Landbase();
OwnerWorkingInterests workingInterests = new OwnerWorkingInterests();
//Owner owner = new Owner();
var query = (from wg in _db.WorkingInterestGroups
join wi in _db.WorkingInterests on wg.Id equals wi.WorkingInterestGroupId
join l in _db.Leases on wg.LeaseId equals l.Id
where wi.OwnerId.Equals(Id)
select new OwnerWorkingInterests()
{
LeaseId = l.Id,
WorkingInterestAmount = wi.WorkingInterestAmount,
WorkingInterestGroupName = wg.Name,
ClientAlias = l.ClientAlias,
Lessor = l.Lessor,
Lessee = l.Lessee,
VolDocNumber = l.VolumeDocumentNumber,
County = l.County,
District = l.District
}).ToList();
//List<string> OwnerWorkingInterest = query.ToList<string>();
return View(query);
}
I have the following code in my view:
<div id="OwnerWorkingInterests" class="tab-pane fade">
<h3>Working Interests</h3>
<table class="table">
<thead>
<tr>
<td>Lease Id:</td>
<td>Working Int:</td>
<td>WI Group Name:</td>
<td>Alias:</td>
<td>Lessor:</td>
<td>Lessee:</td>
<td>VolPg:</td>
<td>County:</td>
<td>District0:</td>
</tr>
</thead>
<tbody>
#foreach (var owi in OwnerWorkingInterests)
{
<tr>
<td>#owi.LeaseId</td>
<td>#owi.WorkingInterestAmount</td>
<td>#owi.WorkingInterestGroupName</td>
<td>#owi.ClientAlias</td>
<td>#owi.Lessor</td>
<td>#owi.Lessee</td>
<td>#owi.VolDocNumber</td>
<td>#owi.County</td>
<td>#owi.District</td>
</tr>
}
</tbody>
</table>
</div>
I thought this would populate the table with the proper information
This is the viewmodel:
namespace LandPortal.Models
{
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.Spatial;
public partial class WorkingInterest
{
public int Id { get; set; }
public int? OwnerId { get; set; }
[Column("WorkingInterest")]
public decimal? WorkingInterestAmount { get; set; }
[StringLength(45)]
public string CreateUser { get; set; }
[StringLength(45)]
public string ModifyUser { get; set; }
public Guid? CreateUserId { get; set; }
public Guid? ModifyUserId { get; set; }
public DateTime? CreateDate { get; set; }
public DateTime? ModifyDate { get; set; }
public int? WorkingInterestGroupId { get; set; }
public WorkingInterestGroup WorkingInterestGroup { get; set; }
public decimal? ORRI { get; set; }
public int? ORRIOwnerId { get; set; }
public virtual Owner Owner { get; set; }
}
}
So what happens is it throws a very vague error when I run it in debugger. It literally just says Error: An error occurred when processing your request. So I am assuming that the list is populating but not working in the foreach in the view. I could be wrong at this point.
Here are the model directives for the view
#using LandPortal.Models
#using LandPortal.ViewModels
#using Microsoft.Ajax.Utilities
#model LandPortal.Models.Owner
If the view expects a model that is of type LandPortal.Models.Owner, and Index returns an entire ActionResult, then Index needs to return a model of that type.
A tiny example:
public ActionResult Index(int Id)
{
Landbase _db = new Landbase();
Owner owner = new Owner();
// some query has to set properties on this owner object
// let's pretend there's a property named OwnerWorkingInterests on it
owner.OwnerWorkingInterests = query.ToList(); // you will have to define "query" and set it similar to how you already did
return View(owner);
}
Now your view can access the property on the model as so
#foreach (var owi in Model.OwnerWorkingInterests)
This is a very high level example, but I see you have a partial class and mentioned partial views in your comment. If you have a large view and are trying to break up a query into pieces, that can be done with PartialViewResult and will be a bit different from this.

inherited classes and razor asp.net mvc 5

Good morning, I have a problem with returning my database information (generated using the EF 6 code-first method) for my View Razor. The issue is that I'm wanting to return the information from inherited classes in the View, but they are not available, only the properties of the base class are presented, not those of the dependent classes.
The following are the Model, Controller, and View classes used:
Class ClientModel
public class Client
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ClientId { get; set; }
[DataType(DataType.Date)]
public DateTime Birth { get; set; }
[Display(Name = "Telefone principal")]
public string Phone1 { get; set; }
[Display(Name = "Telefone Alternativo")]
public string Phone2 { get; set; }
public ICollection<OrcamentoContato> Contacts { get; set; }
public ICollection<Contrato> Contracts { get; set; }
}
Class FisicModel
public class PessoaFisica : Client
{
public TipoPessoa PersonType { get; set; }
[Required]
[Display(Name = "Nome completo*")]
[StringLength(250, ErrorMessage = "O campo é obrigatório.")]
public string Name { get; set; }
public string RG { get; set; }
[Required(ErrorMessage = "O campo CPF é obrigatório.")]
[StringLength(14)]
public string CPF { get; set; }
[Display(Name = "Filiação")]
public string Filiacao { get; set; }
[Display(Name = "Endereço")]
public string Address { get; set; }
}
Class JuridicModel
public class PessoaJuridica : Client
{
public TipoPessoa PersonType { get; set; }
[Required]
[Display(Name = "Razão Social*")]
[StringLength(200, ErrorMessage = "O campo é obrigatório.")]
public string SocialName { get; set; }
[Required]
[Display(Name = "CNPJ*")]
[StringLength(200, ErrorMessage = "O campo é obrigatório.")]
public string CNPJ { get; set; }
[Display(Name = "Inscrição Estadual")]
public string InscricaoEstadual { get; set; }
[Display(Name = "Inscrição Municipal")]
public string InscricaoMunicipal { get; set; }
[Display(Name = "Endereço")]
public string Address { get; set; }
public string ContactWith { get; set; }
}
Controller
public ActionResult Index()
{
var clients = db.Clients.ToList();
return View(clients);
}
Index View
#model IEnumerable<CabinePhoto.Models.Entidades.Client>
<table class="table">
<tr>
<th>
#Html.DisplayNameFor(model => model.Birth)
</th>
<th>
#Html.DisplayNameFor(model => model.Phone1)
</th>
<th>
#Html.DisplayNameFor(model => model.Phone2)
</th>
<th></th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.Birth)
</td>
<td>
#Html.DisplayFor(modelItem => item.Phone1)
</td>
<td>
#Html.DisplayFor(modelItem => item.Phone2)
</td>
</tr>
}
IdentityModel
public DbSet<Client> Clients { get; set; }
public DbSet<PessoaFisica> PessoaFisica { get; set; }
public DbSet<PessoaJuridica> PessoaJuridica { get; set; }
All the information is stored in the same client table, since I'm using the form of inheritance by hierarchy, but in the view only the client model information is returned
I've been able to solve the problem. I'll leave here recorded what I did to solve the problem in respect to inheritance.
First, I created a ViewModel and put two ICollection properties, I modified the controller by adding the queries referring to the client table, but specifically bringing the required types and finally, I passed the ViewModel to the Index.cshtml and I used two foreachs to retrieve the information from According to the type specified, shown below:
ClientesiewModel.cs
public class ClientesViewModel
{
public IEnumerable<PessoaFisica> Fisica { get; set; }
public IEnumerable<PessoaJuridica> Juridica { get; set; }
}
controlle.cs
public ActionResult Index()
{
var cliente_fisico = db.Clientes.OfType<PessoaFisica>().ToList();
var cliente_juridico = db.Clientes.OfType<PessoaJuridica>().ToList();
var cliente = db.Clientes.ToList();
ClientesViewModel clientes = new ClientesViewModel()
{
Fisica = cliente_fisico,
Juridica = cliente_juridico
};
return View(clientes);
}
View Index.cshtml
#model CabinePhoto.ViewModels.ClientesViewModel
<table class="table">
<tr>
<th>
#Html.DisplayName("Nome")
</th>
<th>
#Html.DisplayName("Telefone")
</th>
<th>
#Html.DisplayName("Telefone 2")
</th>
<th></th>
</tr>
#if (Model.Fisica != null || Model.Juridica != null)
{
foreach (var fisica in Model.Fisica)
{
<tr>
<td>
#Html.DisplayFor(modelItem => fisica.NomeCompleto)
</td>
<td>
#Html.DisplayFor(modelItem => fisica.TelefonePrincipal)
</td>
<td>
#Html.DisplayFor(modelItem => fisica.TelefoneAlternativo)
</td>
<td>
#Html.ActionLink("Edit", "Edit", new { id = fisica.ClienteId }) |
#Html.ActionLink("Details", "Details", new { id = fisica.ClienteId }) |
#Html.ActionLink("Delete", "Delete", new { id = fisica.ClienteId })
</td>
</tr>
}
foreach (var juridica in Model.Juridica)
{
<tr>
<td>
#Html.DisplayFor(modelItem => juridica.PessoaContato)
</td>
<td>
#Html.DisplayFor(modelItem => juridica.CNPJ)
</td>
<td>
#Html.DisplayFor(modelItem => juridica.TelefonePrincipal)
</td>
<td>
#Html.ActionLink("Edit", "Edit", new { id = juridica.ClienteId }) |
#Html.ActionLink("Details", "Details", new { id = juridica.ClienteId }) |
#Html.ActionLink("Delete", "Delete", new { id = juridica.ClienteId })
</td>
</tr>
}
}
</table>
Thanks for the help previously assigned
Suppose you have a variable like this in your controller.
Client c = new Client();
If you later wrote
c.ClientId = 1;
it would work perfectly.
Similarly, if you wrote
PessoaFisica p = new PessoaFisica ();
and later
p.Name = "abc";
it would also work.
However, if you wrote
Client c = new PessoaFisica();
c.Name = "abc";
It will fail to compile.
Along the same lines
#model IEnumerable <CabinePhoto.Models.Entidades.Client>
means static type of your Model is a collection of Client objects, it will only allow you to bind to properties defined in Client class.
Entity Framework actually returns the correct types, but you're effectively upcasting everything to Client by the variable type you're storing into and the model definition of the view. Essentially, you just need to cast to the right type. Unfortunately, there's no way to just know what type it should be after it's been upcasted. You'll have to conditionally check:
if (item is PessoaFisica)
{
var pessoaFiscica = (PessoaFisica)item;
// now you can access those derived typed properties off of `pessoaFiscica`
}
You can also use as and rely on the fact that it returns null when something can't be casted:
var pessoaFiscica = item as PessoaFisica;
if (pessoaFiscica != null)
{
// access PessoaFiscica properties
}
With C# 7.0, you can use the pattern matching syntax to streamline it a little:
if (item is PessoaFiscica pessoaFiscica)
{
// use `pessoaFiscica`
}
The pattern matching syntax also allows you use switch blocks, which may make things much easier on you:
switch (item)
{
case PessoaFisica pessoaFisica:
// do something with `PessoaFisica` instance
break;
case PessoaJuridica pessoaJuridica:
// do something with `PessoaJuridica` instance
break;
default:
// do something with generic `Client` instance
break;
}

Viewmodel is created twice

I pass an instance of LoginViewModel to my view (Login)
When I click the submit button on the form another instance of LoginViewModel is created.
I can observe this because I put a breakpoint in the constructor.
Why does this happen and how can I fix it?
#using ViewModels
#model LoginViewModel
<form action="~/Home/VerifyLogin" method="post">
<table>
<tr>
<td>
User Name:
</td>
<td>
#Html.DropDownListFor(m => m.SelectedUserID, Model.UserList);
</td>
</tr>
<tr>
<td>
Password:
</td>
<td>
#Html.TextAreaFor(m => m.SelectedPassword);
</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value ="Login" />
</td>
</tr>
</table>
</form>
Controller
public class HomeController : Controller
{
//
// GET: /Home/
public ActionResult Index()
{
return View();
}
public ActionResult RedirectToLogin()
{
return View("Login", new LoginViewModel());
}
[HttpPost]
public void VerifyLogin(LoginViewModel vm)
{
bool sucess = false;
// some logic
if(sucess)
RedirectToAction("ProjectList", "Project");
}
ViewModel
public class LoginViewModel :BaseViewModel
{
public IEnumerable<User> Users { get; private set; }
public IEnumerable<SelectListItem> UserList { get; private set; }
public int SelectedUserID { get; set; }
public string SelectedPassword { get; set; }
public LoginViewModel()
{
Users = DataService.GetUsers();
UserList = new SelectList(Users, "ID", "Name");
}
public bool Login(string userName, string password)
{
return true;
}
}
One is created here...
return View("Login", new LoginViewModel());
And one is created here
public void VerifyLogin(LoginViewModel vm)
The model binder creates objects and fills them with the parameters it receives from the request. This is normal operation.
To fix your problem, I would do this...
public ActionResult RedirectToLogin()
{
var vm = new LoginViewModel()
{
Users = DataService.GetUsers();
UserList = new SelectList(Users, "ID", "Name");
};
return View("Login", vm);
}
public LoginViewModel()
{
//Removed code
}

Populating a select box in a form using related ID in MVC3

I have a very simple data structure with two models. The first containing UserName, UserQuestion and userLocationID and another with LocationName and LocationID, the locationID in the first table is related to the LocationName the second table. However I've not specified any relationship. I've set up the data structure using the code first method in used here .
I would like to create a form which has two text inputs for a user to enter their name and question and a select box that is populated with all the locationNames from the second table. However I can't seem to create the model that allows me to do so. Do I need to make a separate ViewModel?
Does anyone know of a simple tutorial that will explain how to do this?
I'm quite new at MVC, and the dot net framework. . And I've had a look at this answer but I can't seem to modify it to fit my needs. So Apologies if I'm asking for something really basic.
I can give an example in one controller, one view and three C# classes. To use this code, create an empty MVC2 project in visual studio and add a reference to Entity Framework dll version 4.1. If you need help as to where to put these files I recommend Steve Sanderson's MVC2 book.
public class User
{
public int ID { get; set; }
public string UserName { get; set; }
public string Question { get; set; }
public virtual Location Category { get; set; }
}
public class Location
{
public int ID { get; set; }
public string LocationName { get; set; }
}
Repository
using System.Data.Entity;
using System.Collections.Generic;
using System.Linq;
public class Repository : System.Data.Entity.DbContext
{
public DbSet<User> User { get; set; }
public DbSet<Location> Locations { get; set; }
public Repository()
{
this.Database.Connection.ConnectionString =
#"Server=.;Database=Test;Integrated Security=SSPI";
if (!this.Database.Exists())
{
this.Database.Create();
this.Locations.Add(new Location { LocationName = "Queensway" });
this.Locations.Add(new Location { LocationName = "Shepherds Bush" });
this.SaveChanges();
}
}
public IEnumerable<Location> GetLocations()
{
return this.Locations.Where(x => x.ID > -1);
}
public Location GetLocation(int id)
{
return this.Locations.First(x => x.ID == id);
}
public void SaveUser(User user)
{
this.User.Add(user);
this.SaveChanges();
}
}
Controllers\HomeContoller.cs:
using System.Web.Mvc;
public class HomeController : Controller
{
Repository repo = new Repository();
[HttpGet]
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(User user, int categoryId)
{
user.Category = repo.GetLocation(categoryId);
repo.SaveUser(user);
return View();
}
}
Views\Home\Index.aspx
<%# Page Language="C#" Inherits="System.Web.Mvc.ViewPage<User>" %>
<html>
<body>
<% using (Html.BeginForm())
{%>
Username: <%: Html.TextBoxFor(model => model.UserName) %><br />
Question: <%: Html.TextBoxFor(model => model.Question) %><br />
Location: <select name="categoryId">
<% foreach (var location in new Repository().GetLocations())
{%>
<option value="<%= location.ID %>">
<%= location.LocationName %></option>
<%} %>
<br />
</select>
<p>
<input type="submit" value="Create" />
</p>
<% } %>
</body>
</html>

Resources