Foreach in null - asp.net

I have this model class:
public class UserProfile
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
public string UserName { get; set; }
public DbSet<ForeignExpression> learnedexpressions { get; set; }
}
I create the dbset in the controller:
int userID = (int)Membership.GetUser().ProviderUserKey;
model.ForeignExpressions=db.UserProfiles.Find(userID).learnedexpressions;
Then in the view, I try to write them out:
#foreach (var exp in Model.ForeignExpressions.OrderByDescending(m => m.dateAdded).Take(#numOfExpressions).ToList().OrderBy(m => m.dateAdded))
{
<tr>
<td> #exp.expression</td>
<td> #exp.meaning</td>
<td> "#exp.context"</td>
<td> #exp.dateAdded</td>
</tr>
}
However, Model.ForeignExpressions is null, so I get a runtime error for the foreach (Value cannot be null). I suspect that it's because I don't have any ForeignExpression yet for the current user, so the value of learnedexpressions is null. Am I correct, and what is the best way to handle this? Should I fix it in the view, the controller, or the model?

Make the learnedespressions property virtual so as to allow lazy loading of the collection and change the type of the property from DbSet<T> to ICollection<T>

Test whether ForeignExpressions is null prior to attempting to use it:
#if (Model.ForeignExpressions != null)
{
// foreach
}

Related

ASP.NET MVC - Entity framework Lazing Loading is not retrieving data from related classes, using fluent API

I have two classes, with 1 to many relationship, but when trying to retrieve the list of objects that is related to the Support class, that list comes empty.
public class Support
{
public int SupportId { get; set; }
public string Name { get; set; }
public string Summary { get; set; }
public virtual ICollection<FileDetail> FileDetails { get; set; }
}
public class FileDetail
{
public Guid Id { get; set; }
public string FileName { get; set; }
public string Extension { get; set; }
public int SupportId { get; set; }
public virtual Support Support { get; set; }
}
My Repository:
public abstract class Repository<TEntity> : IRepositoryRead<TEntity>, IRepositoryWrite<TEntity> where
TEntity : Entity, new()
{
public virtual IEnumerable<TEntity> GetAll()
{
return Dbset.ToList();
}
}
My Fluent API config
public class FileDetailConfig : EntityTypeConfiguration<FileDetail>
{
public FileDetailConfig()
{
HasKey(c => c.Id);
Property(c => c.FileName);
Property(c => c.Extension);
HasRequired(c =>c.Support )
.WithMany(c => c.FileDetails )
.HasForeignKey(c => c.SupportId );
ToTable("Support");
}
}
My Controller
public ActionResult Index()
{
return View(__supportRepository.GetAll());
}
My View:
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.Name)
</td>
<td>
#Html.DisplayFor(modelItem => item.Summary)
</td>
<td>
#if (item.FileDetails.Count() == 0)
{
<span>No File</span>
}
else
{
<span>#item.FileDetails.Count() File(s)</span>
}
</td>
<td>
#Html.ActionLink("Edit", "Edit", new { id = item.Id }) |
Delete
</td>
</tr>
}
also tried to configure the fluent api in the FileDetail class, but the same thing remains. The list of filedetains returns empty in view
EDIT:
Thats my Support table
Support Table
Ands my FileDetail table:
FileDetain Table
EDIT:
I think the problem is the query, it looks like the query is not looking for the related data in the other table. I just can not understand why
{SELECT [Extent1].[Id] AS [Id], [Extent1].[Name] AS [Name], [Extent1].[Summary] AS [Summary] FROM [dbo].[Support] AS [Extent1]}
Since FileDetails is virtual, I'm pretty sure it will be lazy loaded. So it wont be in the initial query.
I don't understand this mapping:
HasRequired(c =>c.Support)
.WithMany(c => c.FileDetails)
.HasForeignKey(c => c.SupportId)
I don't see a Support property on your model, can you change it to just:
HasMany(c => c.FileDetails).HasForiegnKey(c => c.SupportId);
And see what you get?
You also can try Include in your query (assuming your not calling ToList(), see below):
return DBset.Include(x => x.FileDetails);
Another thing, this will load the entire table into memory:
return Dbset.ToList();
I'd highly recommend just returning the DBSet instead, further filtering will get passed to the DB:
return DBset;

Model not valid when navigational property is null

I am new to ASP mvc and entity framework
I have a model created by entity framework as
public partial class Privilege
{
public Privilege()
{
this.Role_Privilege_Map = new HashSet<Role_Privilege_Map>();
}
public int PrivilegeId { get; set; }
[Required(ErrorMessage="*")]
[Display(Name = "Privilege Name")]
public string PrivilegeName { get; set; }
[Required(ErrorMessage = "*")]
public Nullable<int> ModuleId { get; set; }
public virtual module module { get; set; }
public virtual ICollection<Role_Privilege_Map> Role_Privilege_Map { get; set; }
}
As you can see module is a navigational property.
I have binded this model to a view as
#for (var i = 0; i < Model.Count(); i++)
{
<tr>
<td>
#Html.DisplayFor(modelItem => modelItem[i].PrivilegeName)
#Html.HiddenFor(modelItem => modelItem[i].PrivilegeId)
#Html.HiddenFor(modelItem => modelItem[i].PrivilegeName)
</td>
<td>
#Html.DisplayFor(modelItem => modelItem[i].module.ModuleName)
#Html.HiddenFor(modelItem => modelItem[i].ModuleId)
#Html.HiddenFor(modelItem => modelItem[i].module.ModuleName)
#Html.HiddenFor(modelItem => modelItem[i].module)
</td>
<td>
#Html.CheckBoxFor(modelItem => modelItem[i].Checked)
#Html.HiddenFor(modelItem => modelItem[i].Checked)
</td>
<td>
#Html.ActionLink("Edit", "OpenEditPrivilegeDialog", "RolePrivilegeMapping",
new { id = Model[i].PrivilegeId },
new { #class = "actionHyperLink edit_Privilege_Link" }) |
#Html.ActionLink("Delete", "DeletePrivilege","RolePrivilegeMapping",
new { id = Model[i].PrivilegeId },
new { #class = "actionHyperLink Delete_Privilege_Link" })
</td>
</tr>
}
I have an Update button for updating this model say Privilege name. Now in my action
public ActionResult UpdateRolePrivilege(IList<One_Track.Models.Privilege> updatedPrivilege)
{
if (ModelState.IsValid)
{
}
else
{
ViewBag.PrivilegeMessage = "Privileges updation Failed.";
}
}
return PartialView("PrivilegePartial", sample.GetPrivilegeNames());
}
Is returning false. I put a breakpoint and come to know that the navigational property is null that could be a reason for model not been valid. How can I surpass this. As you can see in code I have added a hidden field for binding navigational property
Why is this happening? Any help will be appreciated
As per the post ModelState.IsValid == false, why? provided by wiz kid
I come to know that tan exception is occuring there as
The parameter conversion from type 'System.String' to type 'sample.Models.module' failed because no type converter can convert between these types.
at System.Web.Mvc.ValueProviderResult.ConvertSimpleType(CultureInfo culture, Object value, Type destinationType)
at System.Web.Mvc.ValueProviderResult.UnwrapPossibleArrayType(CultureInfo culture, Object value, Type destinationType)
at System.Web.Mvc.ValueProviderResult.ConvertTo(Type type, CultureInfo culture)
at System.Web.Mvc.DefaultModelBinder.ConvertProviderResult(ModelStateDictionary modelState, String modelStateKey, ValueProviderResult valueProviderResult, Type destinationType)}
So I removed the line from view
#Html.HiddenFor(modelItem => modelItem[i].module)
This solved by problem

.net MVC passing linq data from controller to view

I am trying to pass data from controller to view. I have searched the web but could not find a solution.
if I do this it works:
Controller:
var yyy = (from a in Connection.Db.Authorities select a) ;
ViewBag.data = yyy;
View:
#foreach(var item in ViewBag.data)
{
#item.Value
}
But the following code does not work:
Controller:
var yyy = (from a in Connection.Db.Authorities select new {Value = a.Value, TypeCode = a.TypeCode, Return = Calculate(a.Return)}) ;
ViewBag.data = yyy;
View:
#foreach(var item in ViewBag.data)
{
#item.Value
}
It gives "item does not contain a definition for Value" for the view file.
Any help would be great.
Thank you.
-edited: updated the second controller linq query. and corrected the first controller linq query.
It's because You already select Value and Value has no such property as Value. You should change in controller:
var yyy = (from a in Connection.Db.Authorities select a.Value); to
var yyy = (from a in Connection.Db.Authorities select a);
OR change the view to
#foreach(var item in ViewBag.data)
{
#item
}
//////////////////////////////////////////////// EDITS ////////////////////////////////////////////////
Than You should not use anonymous object. You should create ViewModelClass. For Example:
public class AuthoritiesViewModel
{
public string Value { get; set; }
public string TypeCode { get; set; }
public string Return { get; set; }
}
And change your controller:
var yyy = (from a in Connection.Db.Authorities select new AuthoritiesViewModel{ Value = a.Value, TypeCode = a.TypeCode, Return = Calculate(a.Return)});
ViewBag.data = yyy;
and in your view you will be able to use:
<table>
<tr>
<th>Value</th>
<th>TypeCode</th>
<th>Return</th>
</tr>
#foreach(AuthoritiesViewModel item in ViewBag.data)
{
<tr>
<td>#item.Value<td>
<td>#item.TypeCode<td>
<td>#item.Return<td>
</tr>
}
</table>
Also, I have a question to You. Why do You use ViewBag to pass data from controller to view? Why don't You use Model to pass these data to view according to MVC pattern?
//////////////////////////////////////////////// MORE EDITS ////////////////////////////////////////////////
To send more than one query result You can create more complex model. For example:
public class AuthoritiesViewModel
{
public string Value { get; set; }
public string TypeCode { get; set; }
public string Return { get; set; }
}
public class AnotherQueryViewModel
{
public string AnotherQueryValue { get; set; }
public string AnotherQueryTypeCode { get; set; }
public string AnotherQueryReturn { get; set; }
}
public class ModelClass
{
IEnumerable<AuthoritiesViewModel> Authorities { get; set; }
IEnumerable<AnotherQueryViewModel> AnotherQueryResults { get; set; }
}
And change the controller:
var yyy = (from a in Connection.Db.Authorities select new AuthoritiesViewModel{ Value = a.Value, TypeCode = a.TypeCode, Return = Calculate(a.Return)});
// do your another select
var zzz = (from smthing select new AnotherQueryViewModel ...)
// create model instance
ModelClass model = new ModelClass()
{
Authorities = yyy.AsEnumerable(),
AnotherQueryResults = zzz..AsEnumerable()
}
// return view with model
return View("view", model);
and in view you can use:
#model ModelClass
#*display first query result*#
<table>
<tr>
<th>Value</th>
<th>TypeCode</th>
<th>Return</th>
</tr>
#foreach(AuthoritiesViewModel item in Model.Authorities)
{
<tr>
<td>#item.Value<td>
<td>#item.TypeCode<td>
<td>#item.Return<td>
</tr>
}
</table>
#*display second query result*#
<table>
<tr>
<th>Another Query Value</th>
<th>Another Query TypeCode</th>
<th>Another Query Return</th>
</tr>
#foreach(AnotherQueryViewModel item in Model.AnotherQueryResults)
{
<tr>
<td>#item.AnotherQueryValue<td>
<td>#item.AnotherQueryTypeCode<td>
<td>#item.AnotherQueryReturn<td>
</tr>
}
</table>
use sth like this
ViewBag.qualification = new SelectList(db.Lookups.Where(x => x.lookup_type == "Qualification"), "lookup_content", "lookup_content");

asp net mvc3 post a list of objects to action

I created a page with aspnet mvc3. It show all users info as a list. I want to do something with this list. There are some checkboxes that belong to each items. When I click some checkboxes and press submit button, I want to post the whole list as a collection and save each items of this collection to database. There are several notes on internet but there is no exact solution. I have a UserDto. and want to use this to transfer users data in all sections.
Does anyone have any full solution about this or can they give any idea?
Thanks in advance.
Kerem
I added some of my codes. You can see the lead sentences what they are about.
this is my index view detail:
#model List<DomainModel.UserApprovalDto>
#{
ViewBag.Title = "Manage Users";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>
Manage Users</h2>
<div>#Html.Partial("_PartialManageUsers", (List<DomainModel.UserApprovalDto>)Model) </div>
this is my partial view detail:
#model List<DomainModel.UserApprovalDto>
#using (Html.BeginForm("ConfirmUsers", "ManageUsers", FormMethod.Post))
{
<table>
<tr>
<th>
Name
</th>
<th>
Is Reported
</th>
</tr>
#for (int i = 0; i < Model.Count(); i++)
{
<tr>
<td>
#Html.DisplayFor(modelItem => Model[i].FirstName)
</td>
<td>
#Html.CheckBox("IsReported", Model[i].IsReported.HasValue ? Model[i].IsReported.Value : false)
#*#Html.CheckBoxFor(modelItem => Model[i].IsReported.Value);*# #* #if (Model[i].IsReported != null)
{
#Html.CheckBoxFor(modelItem => Model[i].IsReported.Value);
}
else
{
#Html.CheckBoxFor(modelItem => Model[i].IsReported.Value);
}*#
</td>
<td>
</td>
</tr>
}
</table>
<div>
<input name="submitUsers" type="submit" value="Save" />
</div>
}
this is my controller submit method
[HttpPost]
public ActionResult ConfirmUsers(List<DomainModel.UserApprovalDto> collection)
{
if (ModelState.IsValid)
{
//TO-DO
}
return RedirectToAction("Index");
}
this last one is my DTO class detail:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DomainModel
{
public class UserApprovalDto
{
public long UserId { get; set; }
public Guid CarUserId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string PhotoPath { get; set; }
public string PhotoSmallPath { get; set; }
public string PhotoSquarePath { get; set; }
public string PhotoBigPath { get; set; }
public bool IsBlocked { get; set; }
public bool IsDeleted { get; set; }
}
}
when I submit this code my list return null collection to my controller method.
thanks for your comments.
Assuming you are creating a screen which adds/ remove users to a course. So let's create some viewmodels
public class CourseVM
{
public string Name { set;get;}
public int CourseID { set;get;}
public List<UserVM> Users { set;get;}
public CourseVM()
{
Users=new List<UserVM>();
}
}
public class UserVM
{
public string Name { set;get;}
public int UserID{ set;get;}
public bool IsSelected { set;get;}
}
Now in your GET Action, you will fill the values of the ViewModel and sent it to the view.
public ActionResult Add()
{
var vm = new CourseVM();
//The below code is hardcoded for demo. you may replace with DB data.
vm.Users.Add(new UseVM { Name = "Jon" , UserID=1});
vm.Users.Add(new UseVM { Name = "Scott", UserID=2 });
return View(vm);
}
Now Let's create an EditorTemplate. Go to Views/YourControllerName and Crete a Folder called "EditorTemplates" and Create a new View there with the same name as of the Property Name(UserVM.cshtml)
Add this code to your new editor template.
#model ChannelViewModel
<p>
<b>#Model.Name</b> :
#Html.CheckBoxFor(x => x.IsSelected) <br />
#Html.HiddenFor(x=>x.Id)
</p>
Now in your Main View, Call your Editor template using the EditorFor Html Helper method.
#model CourseVM
#using (Html.BeginForm())
{
<div>
#Html.EditorFor(m=>m.Users)
</div>
<input type="submit" value="Submit" />
}
Now when you post the form, Your Model will have the Users Collection where the Selected Checkboxes will be having a True value for the IsSelected Property.
[HttpPost]
public ActionResult Add(CourseVM model)
{
if(ModelState.IsValid)
{
//Check for model.Users collection and Each items
// IsSelected property value.
//Save and Redirect(PRG pattern)
}
return View(model);
}

Dynamic link issue in an MVC 3 website

I'm creating my first ASP.NET MVC 3 website for my company's intranet. It's a pretty cool, I play audio recorded by our phone system and saved in our db. That's working good, but I'm having a hard time figuring out how to do something that should be simple. Please forgive any syntax errors I most likely have, this is a rough draft.
I have a table in the Index View /Apps that list all the AppName's, and next to each AppName I want to display a link to another view, with the text of the link being a Count() of all CallDetails associated with that App.
I have two classes:
public class Apps
{
public int AppId { get; set; }
public string AppName { get; set; }
}
public class CallDetail
{
public int Id { get; set; }
public int AppID { get; set; }
public byte[] FirstName { get; set; }
public byte[] LastName { get; set; }
....etc
}
a context for each:
public class AppsContext : DbContext
{
public DbSet<Apps> Apps { get; set; }
}
public class CallContext : DbContext
{
public DbSet<CallDetail> CallDetails { get; set; }
}
a controller method for each:
// AppsController
private AppsContext db = new AppsContext();
public ViewResult Index()
{
return View(db.Apps.ToList());
}
// CallController method (from my current attempt)
public ActionResult CallCheck(int id)
{
bool? enabled = null;
var appcalls = from s in db.CallDetails
where s.AppID == id
&& s.Enabled.Equals(enabled)
select s;
string callnum = appcalls.Count().ToString();
return View(callnum);
}
It displays the AppName just fine in this portion of the View below, and I can create a link to a View for each associated CallDetail just fine. But I don't know how to display info I'd get from the CallDetail Controller since the View's Model is Apps and its Controller, AppsController.
#model IEnumerable<myMessagePlayer.Models.Apps>
...
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.AppName)
</td>
<td class="appLink">
...
</td>
</tr>
}
I've tried many different methods, some that I might have gotten to work, but they seemed semantically un-MVC. So I figured I'd just ask a general "whats the standard practice?" type of question.
The path you are currently going down would end up hitting the database for each app you have in your database. There is a way to display all the information with only one hit to the database.
Your context needs to change to this:
public class ApplicationContext : DbContext
{
public DbSet<Apps> Apps { get; set; }
public DbSet<CallDetail> CallDetails { get; set; }
}
You could create a view model object called AppCallInfo that has three properties:
public class AppCallInfo
{
public int AppID { get; set; }
public string AppName { get; set; }
public int CallCount { get; set; }
}
In your Index action you need to do something like this:
public ViewResult Index()
{
var model = from a in db.Apps
join c in db.CallDetails on a.AppID equals c.AppID
where c.Enabled == enabled
group a by new { AppName = a.AppName, AppID = a.AppID } into g
select new AppCallInfo {
AppName = g.Key.AppName,
AppID = g.Key.AppID,
CallCount = g.Count()
};
return View(model.ToList());
}
Now you have everything you need for each row in your table in one object.
#model List<myMessagePlayer.ViewModels.AppCallInfo>
...
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.AppName)
</td>
<td class="appLink">
#Html.ActionLink(item.CallCount, "ViewCalls", "Call", new { Id = item.AppID }, null)
</td>
</tr>
}
Using this method avoids hitting the database for each app you have in your table.
Is the view CallCheck a partial view?
In your index view you could use
#Html.RenderAction("CallCheck", "AppsController", new { Id = #Model.AppId } )
The syntax may not be 100% correct, but it should get you going in the right direction.

Resources