I dont know why the "ViewModels" folder reference is missed on my MVC5 login.cshtml file. Please checkout two screen shot to get clear idea about error.
Inside your "ViewModels" folder please check "User.cs" and "UserLogin.cs" class. I think you didn't used its namespace correctly. That's why MVC may not recognising your "ViewModels" folder in reference. The test code bellow you can put on "User.cs" class. I think after you replace "User.cs" codes with mine your "ViewModels" folder should be listed.
User.cs code:
namespace WebApplication4.ViewModels
{
public class User
{
public int UserId { get; set; }
public string Email { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
}
You should try:
#using WebApplication4.ViewModels
if you wanna use namespace.
For model you have to pass Class not namespace.
For example
#model WebApplication4.ViewModels.UserLogin
for your UserLogin class. And please check if your class is in right namespace or paste code from UserLogin.cs
Related
I want to add a new property on my class, make it strongly typed so I can use it in my views and controllers, I've tried to inherit the properties, but Entity Framework or C# throws me errors...
I have this class:
public class Patient
{
public int Id { get; set; }
public string Message { get; set; }
public string FirstName { get; set; }
.....
}
which has a lot more properties in it, but shortened here.
I have a razor view, which is uses 'Patient' as it's model
using model Project.Models.Patient
So I had completed my view (or so I thought) and was asked to add functionality in the view. The functionality is to send a POST using a form of a 'Message' (a simple textarea in html). I've already got all the details I want, but this new 'Message'
So I thought, because I don't want this field in the database I could add it like this:
public class Patient
{
public int Id { get; set; }
public string Message { get; set; }
public string FirstName { get; set; }
[NotMapped]
public string Message { get; set; }
.....
}
But I'm not a fan of this, it doesn't relate to the Patient in any other way.
So I thought I could change my model in razor to something like this:
#model Project.Models.DTOs.PatientMessage
and inherit the Patient class and all it's properties (so I don't have to retype and copy past the fields again) and the new PatientMessage class would look like this:
public class PatientMessage : Patient
{
public string Message { get; set; }
}
But when I refresh my application, I receive a message stating the Application Database Context has changed, and I have to update this. I don't want to update my database, and I can't really see why I need to, it's an extra field which I don't want to include in my database.
So then I decided to make this class an 'abstract' class
public abstract class PatientMessage : Patient
{
public string Message { get; set; }
}
When I refreshed my page this time, I saw no need to update the Database, great I thought, and when I went near a page where the model was
#model Project.Models.Patient
I received this message
The abstract type 'Project.Models.DTOs.PatientMessage' has no mapped descendants and so cannot be mapped. Either remove 'Project.Models.DTOs.PatientMessage' from the model or add one or more types deriving from 'Project.Models.DTOs.PatientMessage' to the model.
MY QUESTION
Can I include this one field, without placing it on the Patient class, ideally without having to update models in my razor views, or would I have to change the models in the views and controllers and update the information to include the message and map all the details from a 'PatientMessage' to a 'Patient'
Please let me know if you need any further information.
Regards
I have created a DisplayTemplate folder within shared folder which has a Url view that looks like this:
#ViewData.Model
This is what properties in my class looks like:
public class Employee
{
[ScaffoldColumn(false)]
public int EmployeeId { get; set; }
public string WebsiteName { get; set; } //i want this as display text of link
[DataType(DataType.Url)]
public string website { get; set; } //this should be the url to which user will be redirected to
}
I am aware that I can do it in other ways but I want to do it using the DisplayTemplate if possible.
In your DisplayTemplate place the following at the top of the file.
#model Employee
Once you've done that, you have access to its properties.
So you can do
#Model.WebsiteName
If your Employee.cshtml is in partial view, you really won't be able to access your properties such as phonenumber etc. You'll only be able to access those properties on your main page, but not on partial view. On your partial view you may only access the properties of its model.
The answer of JasonIPrice is technically correct.
P.S. I can't comment on your answer JasonlPrice.
I have 2 viewmodels each of them have a property called RSSFeed of type RSS. I have 2 views (one for each viewmodel) and I want them to share the same partialview which renders the data in RSSFeed.
In my main views, I have:
#{ Html.RenderPartial("Social", new ViewDataDictionary(Model)); }
In my Social partiavlview I wanted to use something like:
#ViewBag.RSSFeeds.Feed[0].Title
Running that give me 'Cannot perform runtime binding on a null reference'
Each ViewModel has:
public RSS RSSFeed = new RSS();
The supporting classes are:
public class RSSItem
{
public string Title { get; set; }
public string Link { get; set; }
public string Description { get; set; }
public DateTime PubDate { get; set; }
}
public class RSS
{
public string FeedURL { get; set; }
public List<RSSItem> Feed = new List<RSSItem>();
}
Any help is greatly appreciated. I wonder if I am actually able to do what I am looking to do or have completely done this the wrong way.
In your example above, .Feed is an empty List of RSSItems. Your code:
#ViewBag.RSSFeeds.Feed[0].Title
If attempting to get the title from the first Feed in your (empty) list which is why you are getting the null reference error.
You'd need to do a check in your partial to make sure that a Feed exists before using it.
#if (ViewBag.RSSFeeds != null && ViewBag.RSSFeeds.Feed.Count() > 0)
If you use the ViewBag, you'll need to populate that on every page load that uses it - if it's only 2 pages that might not be a problem. Maybe it would be better to use the Session to store your RSSFeeds instead?
Set
Session["RSSFeeds"] = RSSFeeds;
Get
if (Session["RSSFeeds"] != null)
return (RSSFeeds)Session["RSSFeeds"];
else
return null;
I can't comment yet, but are the inputs you are using for RenderPartial correct? on MSDN there is no method that takes a ViewDataDictionary and one other object.
I have a simple class below and I like to add it as a data type in setting file as I can see it in the type list of setting designer:
namespace MyApp.Classes
{
class BankCash
{
public string BankName { get; set; }
public string BankNumber { get; set; }
public BankCash(string bankName, string bankNumber)
{
BankName = bankName;
BankNumber = bankNumber;
}
}
}
Any Idea? Thanks
Based on what I found in this post it appears the class must be in a different assembly than the one you are creating the settings class for.
I also experimented with it a bit and it appears if you don't have a no-arg constructor the class won't show up either.
P.S. I'm assuming the constructor is a typo because the class is named Bank but the constructor is BankCash.
I have a Entity class (auto-generated) that looks like this:
namespace FicServerData
{
using System;
using System.Collections.Generic;
public partial class Snapshot
{
public Snapshot()
{
this.ComponentQuotes = new HashSet<SnapshotPart>();
}
public int Id { get; set; }
public System.DateTime Time { get; set; }
public string Machine { get; set; }
public string AppName { get; set; }
public virtual ICollection<SnapshotPart> ComponentQuotes { get; set; }
}
}
Then I wrote a utility partial class to looks like this:
namespace FicServerData
{
public partial class Snapshot
{
public IEnumerable<Quote> DerivedQuotes
{
get
{
...
}
}
}
}
So those are both files in the same project and as you can see in the same namespace. Now inside this project I can access the property i added no problem. I can't access it from a project that references it though: i only have access to the Entity class VS has created for me.
What am I missing here?
You cannot access partial classes across projects. The 'parts' of partial classes are compiled to a single class within a single assembly.
The fix for me was to restart the Visual Studio. I am at version Professional 2017.
I had this problem. I found that my consuming project was referencing the source project via an assembly reference rather than a project reference. Further, the assembly being referenced was stored in a drive folder, and that drive folder was added as a reference path in the consuming project. As you would expect, my updates in the source project were compiling, but my referenced assembly was not. I deleted the reference in my consuming project and re-added it as a project reference, et voila! Problem solved!
Don't ask me why my consuming project was using a refence path to access a compiled assembly from another project in the same solution. Please, don't.