Accessing objects inside model passed to Razor View in ASP.NET MVC 3 - asp.net-mvc-3

My beer model has a brewery object but I can't access the brewery's properties when I'm in the view. Intellisense seems to think I can access the brewery property but the DisplayFor method doesn't print anything.
#model BeerRecommender.Models.Beer
#{
ViewBag.Title = "Details";
}
<h2>#Html.DisplayFor(model => model.Name)</h2>
<fieldset>
<legend>Details</legend>
<div class="display-label">Style: #Html.DisplayFor(model => model.Style.Name)</div>
<div class="display-label">Brewery: #Html.DisplayFor(model => model.Brewery.Name)</div>
</fieldset>
Here is the Beer class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using BeerRecommender.Models.ViewModels;
namespace BeerRecommender.Models
{
public class Beer
{
[Key]
public int BeerID { get; set; }
public ICollection<City> Cities { get; set; }
public ICollection<Tag> Tags { get; set; }
public Style Style { get; set; }
public string Name { get; set; }
public Brewery Brewery { get; set; }
}
...and here is the Brewery class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.ComponentModel.DataAnnotations;
namespace BeerRecommender.Models
{
public class Brewery
{
[Key]
public int BreweryID { get; set; }
public string Name { get; set; }
public ICollection<Beer> Beers { get; set; }
public City City { get; set; }
}
}

How are you loading the Beer class? If this is from say entity framework you will need to call .Include(o=>o.Brewery) in your loading code to include that table . This is assuming EF 4.1 otherwise you need the string name like .Include("Brewery") if in 4.0

Related

Cant use system.web.security and HttpContext.Current.User.Identity.Name in dot net core 6

I'm trying to migrate from ASP.NET MVC on .NET 4.5 to ASP.NET Core 6,
I can't use System.Web.Security any more.
I also can't use HttpContext.Current.User.Identity.Name. Please suggest an alternative to implement the same functionality:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using Ibq.Wages.ManagementSystem.Models;
using Microsoft.AspNetCore.Http;
namespace Ibq.Wages.ManagementSystem.AppStart
{
public class CustomRoleProvider : RoleProvider
{
public int UserCompanyId { get; set; }
public string PayerQid { get; set; }
public string PayerEid { get; set; }
public bool OTPCompleted { get; set; }
public int? AuthorizationLevels { get; set; }
private PIBQWS_prodContext context = new PIBQWS_prodContext();
public override string ApplicationName { get; set; }
public CustomRoleProvider()
{
if(String.IsNullOrEmpty(HttpContext.Current.User.Identity.Name) == false)
UserCompany(HttpContext.Current.User.Identity.Name);
}
}
}

JsonRequestBehavior.AllowGet does not exist in current context

I am using asp.net MVC core and I am going to read data from SQL in kendo drop-down list.
I have installed Newtonsoft.Json library too. I see drop-down list but I can't load data in my drop-down list. my code is as below:
my model is located in models>Airports.cs:
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace plan_1.Models
{
public class Airport: BaseEntity
{
public int Id { get; set; }
public string Iata { get; set; }
public string Icao { get; set; }
public string LogoUrl { get; set; }
public int IsBased { get; set; }
public int CityId { get; set; }
public virtual City City { get; set; }
public Airport()
{
this.Terminals = new
HashSet<Terminal>();
}
public ICollection<Terminal> Terminals
{ get; set; }
}
}
My controller is located in Controllers>planController.cs :
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using plan_1.Models;
using Newtonsoft.Json;
namespace plan_1.Controllers
{
public class planController : Controller
{
public ActionResult Index()
{
return View();
}
public JsonResult GetAirPort()
{
plan_1Context dbContext = new
plan_1Context();
return
Json(dbContext.AirPorts.Select(O =>
new { _Id = O.Id, Origin = O.Iata
}),
JsonRequestBehavior.AllowGet);
}
}
}
and my view located in views>plan>index.cshtml is as below:
#model IEnumerable<plan_1.Models.Airport>
#{
ViewData["Title"] = "Planing";
}
<div>
<h4>Origin:</h4>
#(Html.Kendo().DropDownList()
.Name("Origin")
.HtmlAttributes(new { style
= "width:100%" })
.OptionLabel("Select
category...")
.DataTextField("Iata")
.DataValueField("Id")
.DataSource(source =>
{
source.Read(read =>
{
read.Action("GetAirPorts",
"planController");
});
})
)
</div>
also, I should mix the airplane model by
the plan model, I think I should use the
view model to mix them.
Please help what should I do? It is days
that I am looking for the answer
.DataTextField("Iata")
.DataValueField("Id")
Is what you have defined as the id and the value in the dropdown however you are passing _Id and Origin back from the controller. I suggest changing either of these to match so that it can bind to the model. I also suggest having .AutoBind(true) in your dropdown list.

Why can't I use the Column data annotation with Entity Framework 5?

I want to use the Column data annotation as shown in the sample code below but the compiler (and also IntelliSense) do not seem to know that particular data annotation. I'm using EF 5 in Visual Studio 2010. I installed EF 5 using NuGet. The Required and MaxLength annotations are working.
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace Model
{
public class Destination
{
public int DestinationId { get; set; }
[Required]
public string Name { get; set; }
public string Country { get; set; }
[MaxLength(500)]
public string Description { get; set; }
[Column(TypeName="image")]
public byte[] Photo { get; set; }
public List<Lodging> Lodgings { get; set; }
}
}
What am I missing?
Column is in:
using System.ComponentModel.DataAnnotations.Schema;
the following code:
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
namespace ConsoleApplication2
{
public class MyContext : DbContext
{
public IDbSet<Entity> Entities { get; set; }
}
public class Entity
{
public int Id { get; set; }
[Column(TypeName = "image")]
public byte[] Photo { get; set; }
}
}
produces:

Difficulty using "public virtual" variables in database creation

I am creating two tables in Visual Web Developer 2010 Express using the following code:
http://imgur.com/a/Mi2Bv (sorry, the forum will not let me, due to a new account, post pictures directly)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace BarApp.Models
{
public class Drinks
{
public int DrinksId { get; set; }
public int EstablishmentsID { get; set; }
public string name { get; set; }
public decimal price { get; set; }
public string description { get; set; }
public string image { get; set; }
public virtual Establishments establishment { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace BarApp.Models
{
public class Promotions
{
public int PromotionsId { get; set; }
public string name { get; set; }
public float discount { get; set; }
public int EstablishmentId { get; set; }
public int DrinkId { get; set; }
public string description { get; set; }
public virtual Establishments establishment { get; set; }
public virtual Drinks drink { get; set; }
}
}
But when I look at the created tables, the Promotions table has actual rows for the public virtual code, whereas the Drinks table does not.
I am able to have the Drinks table function the way I want elsewhere in the project, but I cannot get Promotions to behave the same way because it appears that "public virtual" is giving different results in each table.
I do not understand why my Promotions table is actually creating rows for the public virtual variables. Can someone help me understand?
In this particular situation I was not using the conventional way of referencing the Id field of another table.
I simply needed to change the following:
public int EstablishmentId { get; set; }
public int DrinkId { get; set; }
to the following:
public int EstablishmentsId { get; set; }
public int DrinksId { get; set; }
My tables were created as expected after I made this change.

How to make the entity key value user-assigned

I'm retrieving data from another database that has already created unique IDs for each "beer" entity. However, when I assign the unique key value from the remote database to a new "beer" object, it gets replaced as soon as the object is inserted into the database.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using BeerRecommender.Models.ViewModels;
namespace BeerRecommender.Models
{
public class Beer
{
public Beer()
{
Created = DateTime.Now;
Updated = DateTime.Now;
}
[Key]
public int ID { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public Brewery Brewery { get; set; }
public DateTime Created { get; set; }
public DateTime Updated { get; set; }
public ICollection<City> Cities { get; set; }
public ICollection<Tag> Tags { get; set; }
public Style Style { get; set; }
}
}
I'm using the UnitOfWork pattern.
UnitOfWork.BeerRepository.Insert(beer);
UnitOfWork.Save();
As you already discovered, EF will generate values for int primary keys. To fine tune this behavior you need to mark your key with the DatabaseGenerated attribute (or you can also configure this with the the fluent api's HasDatabaseGeneratedOption method):
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int ID { get; set; }

Resources