ModelState is false for relations with required fields - asp.net

I have two models Invoice and Client which has one to many relation. My problem is when I try to create a new invoice the modelstate becomes false because the required fields of the Client object are not filled. I tried to set default values e.g. int property {get; set;} = 1; but this causes to override the property value when I try to retrieve the relational model. I'm new to asp.net any help would be appreciated.
Here is my Client model code:
public class Client
{
public int ClientId { get; set; }//Primary key
[Required]
[Display(Name = "Client/Company Name")]
public string Name { get; set; }
//.....
public List<Invoice> Invoices { get; set; }
}
Invoice Model:
public class Invoice
{
public int InvoiceId { get; set; }//Primary key
[Required]
[Display(Name = "Client/Company Name")]
public int ClientId { get; set; }//Foreign key
public Client client { get; set; }
//....
}
My code to save an invoice:
[HttpPost]//Save quote & redirect to edit quote
public IActionResult Create(Invoice quote)
{
ViewData["Title"] = "Create Quotation";//Set title of the view!
if (ModelState.IsValid)//If quote is validated to true.
{
_context.Invoices.Add(quote);//insert quote
_context.SaveChanges();
//Redirect to edit page to add items to the invoice
return RedirectToAction("Edit", new { id = quote.InvoiceId });
}
//...
}
My code to retrieve invoices with their client names
public IActionResult Index()
{
ViewData["Title"] = "Quotations";//Set title of the view!
//Return with array of all quotations.
return View(_context.Invoices.Include(i => i.client).Where(i => i.isQuote == true).ToArray());
}
Any hints or help would be appreciated.

Found a simpler solution:
Just add virtual keyword and GG
in Invoice Model:
public class Invoice
{
public int InvoiceId { get; set; }//Primary key
[Required]
[Display(Name = "Client/Company Name")]
public int ClientId { get; set; }//Foreign key
public virtual Client client { get; set; }
//....
}

Related

How Do I set the Guid Id of a User to an item that they are creating as foreign key?

I am building a project on ASP.Net for an assignment and I'm having trouble figuring out how to add the currently logged in user's Id which is a guid as a foreign key to know what items they are adding. I need to do this because when the user is logged in, they need to see their uploaded files only.
The following is the code that I have tried:
The Business Logic Layer:
public void AddAudio(string title, string description, int genre, Guid userId, string filePath)
{
Audio i = new Audio();
i.Title = title;
i.Description= description;
i.Genre_Id = genre;
i.User.Id = userId;
if (string.IsNullOrEmpty(filePath) == false)
i.FilePath = filePath;
new AudioRepository().AddAudio(i);
}
The following is the Controller:
public ActionResult Create(Audio i, HttpPostedFileBase fileData)
{
try
{
Logger.LogMessage("", Request.Path, "Entered the Create
Action");
string uniqueFilename = Guid.NewGuid() +
Path.GetExtension(fileData.FileName);
string absolutePath = Server.MapPath(#"\Audio") + #"\";
fileData.SaveAs(absolutePath + uniqueFilename);
i.FilePath = #"\Audio\" + uniqueFilename;
new AudioBL().AddAudio(i.Title, i.Description,i.Genre_Id,
i.User.Id, i.FilePath);
Logger.LogMessage("", Request.Path, "Finished adding the item in db");
TempData["message"] = "Item added successfully";
return RedirectToAction("Index");
}
I have also added a picture of my Model so that you can understand my question better. :
Your code look fine
when you do i.User.Id = userId; that a relationship
if that not works maybe your model not good or the data not correct,
your model need to be something like this:
public class User
{
[Key]
public Guid Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public string Password { get; set; }
public string NoOfAttempts{ get; set;}
public string Mobile { get; set; }
public bool Blocked { get; set; }
public ICollection<Audio> Audios { get; set; }
}
public class Audio
{
[Key]
public Guid Id { get; set; }
public string Genre_Id {get;set;}
public string Title { get; set; }
public string Description { get; set; }
public string FilePath { get; set; }
//FK
public Guid UserId { get; set; }
public virtual User User { get; set; }
}

asp.net - LINQ Query with relational Data

I have two tables Category and Document. See relationships in picture
See picture
I wrote the following query to select data from both tables based on relationship
public List<DocumentViewModel> All()
{
var docs = _context.Document.ToList();
List<DocumentViewModel> docList = docs.Select(x => new DocumentViewModel
{ DocumentId = x.DocumentId,
DocumentPath = x.DocumentPath,
CategoryId = x.CategoryId,
CategoryName = x.Category.CategoryName }).ToList();
return docList;
}
when this function is called , I get the following error
System.NullReferenceException: 'Object reference not set to an instance of an object.'
Here are my modals
document
public class Document
{
[Key]
public int DocumentId { get; set; }
[Required]
public string DocumentPath { get; set; }
public Nullable<int> CategoryId { get; set; }
public virtual Category Category { get; set; }
}
Category
public class Category
{
[Key]
public int CategoryId { get; set; }
[Required]
public string CategoryName { get; set; }
public virtual ICollection<Document> Documents { get; set; }
}
DocumentViewModel
public class DocumentViewModel
{
public int DocumentId { get; set; }
public string DocumentPath { get; set; }
public int? CategoryId { get; set; }
public string CategoryName { get; set; }
}
Any Idea where am doing mistake?
In this case there is no reason to get a List in memory and then do the projection, you can do this directly from EF instead. Even if there is no relationship defined EF will return null for CategoryName if you project the the results. If you go to memory first then an NRE is expected if there is no Category relationship.
public List<DocumentViewModel> All()
{
return _context.Document.Select(x => new DocumentViewModel
{ DocumentId = x.DocumentId,
DocumentPath = x.DocumentPath,
CategoryId = x.CategoryId,
CategoryName = x.Category.CategoryName}).ToList();
}
Original reason why it is failing.
There is at least one entity that does not have a corresponding relationship with Category.
You do not have lazy loading enabled (which is a good thing) and if that is the case you should use Include to return the relationship.

Field is required. even after filling the field - Validation

I am following this course, and the instructor added code to add records to the DB. The class that's going to be added to the DB looks like this:
public class Gig
{
public int Id { get; set; }
public ApplicationUser Artist { get; set; }
[Required]
public string ArtistId { get; set; }
public DateTime DateTime { get; set; }
[Required]
[StringLength(255)]
public string Venue { get; set; }
public Genre Genre { get; set; }
[Required]
public byte GenreId { get; set; }
}
And there's a view model that's attached to the view, to do the mapping, and it looks like this:
public class GigFormViewModel
{
[Required]
public string Venue { get; set; }
[Required]
[FutureDate]
public string Date { get; set; }
[Required]
[ValidTime]
public string Time { get; set; }
[Required]
public byte Genre { get; set; }
[Required]
public IEnumerable<Genre> Genres { get; set; }
public DateTime GetDateTime() => DateTime.Parse($"{Date} {Time}");
}
I have a create method, that gets form fields, and does the mapping from view model to the model itself, and then tries to add the records to the DB, my create action looks like this:
[Authorize]
[HttpPost]
public ActionResult Create(GigFormViewModel viewModel)
{
viewModel.Genres = _context.Genres.ToList();
if (!ModelState.IsValid)
{
return View(viewModel);
}
var gig = new Gig()
{
GenreId = viewModel.Genre,
ArtistId = User.Identity.GetUserId(),
DateTime = viewModel.GetDateTime(),
Venue = viewModel.Venue
};
_context.Gigs.Add(gig);
_context.SaveChanges();
return RedirectToAction("Index", "Home");
}
On the submit, I get the viewModel's property Genres and populate it with the records from the DB, then I check for ModelState.IsValid. but it's giving me:
Genres Field is required
Although I set it just 2 lines above.
Is there something wrong am doing here? Any guidance is appreciated.
Thanks.
You don't need to add a validation data annotation to Genres property in your view model because it is not a data that is set by users but it is set by you to help your view to get a collection of Genre and populate something like a dropdown list.
Firstly, remove the [Required] attribute that decorates your Genres property in GigFormViewModel.
Secondly, refactor your action method, specially the if bloc like below:
[Authorize]
[HttpPost]
public ActionResult Create(GigFormViewModel viewModel)
{
if (!ModelState.IsValid)
{
// re-populate Genres collection only is tha data is in invalid state.
viewModel.Genres = _context.Genres.ToList();
return View(viewModel);
}
// The remainder code does not change
}

How to handle validation for Relationship PK and FK in MVC5

Hi I have 2 table name tblGroup and tblSubGroup and tblGroup has GroupId which is primary and tblSubGroup has Groupid which is foreign key.
Below are the model generated for them
tblGroup Model
public partial class tblGroup
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public tblGroup()
{
this.tblSubGroups = new HashSet<tblSubGroup>();
}
public int GroupID { get; set; }
[Required(ErrorMessage = "Group Name is Required")]
public string Title { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<tblSubGroup> tblSubGroups { get; set; }
}
tblSubGroup Model
public partial class tblSubGroup
{
public int SubGroupID { get; set; }
[Display(Name = "tblGroup")]
public int GroupID { get; set; }
[Required(ErrorMessage = "SubGroup Name is Required")]
public string Title { get; set; }
public virtual tblGroup tblGroup { get; set; }
}
Now on deleting record of From GroupTable it is giving issue. Instead I need to validate a message that "This record is bind with another table or entity. So it cannot be deleted". I need to show this kind of message.
As I am new I don't know this things is possible or not
Since you need to verify with the database you move this type of validation to the server.
[HttpPost]
public ActionResult Delete(Group group)
{
var grp = db.Group.FirstOrDefault(g => g.Id == group.Id);
if (HasSubGroups(grp))
{
ModelState.AddError("DeleteValidation", "Cannot delete while sub-groups exists");
return View(group);
}
// delete normally ...
}
Then you could display the errors on the view in several ways. The simplest is just to show the collection.
#Html.ValidationSummary()

ASP.NET MVC Relational Model Error

I have a two relational Model first one is
Teacher.cs
public class Teachers
{
[Key]
public int TeacherID { get; set; }
public string TeacherName { get; set; }
public string TeacherLname { get; set; }
public int DepartmentID { get; set; }
public string Image { get; set; }
public Department Department { get; set; }
}
and second is Department.cs
public int DepartmentID { get; set; }
public string DepartmentName { get; set; }
public string Image { get; set; }
public List<Teachers> Teachers { get; set; }
When I'm creating a new record, I' choose a Department Name for teacher, and It's adding fine. But When I want to Delete a record there is a error like this
The ViewData item that has the key 'DepartmentID' is of type 'System.Int32' but must be of type 'IEnumerable<SelectListItem>'.
Line 32: #Html.DropDownList("DepartmentID", String.Empty)
I don't understand what I need to do. Can you help me?
Thanks a lot
TeacherController
EDIT :
//
// GET: /Teachers/Delete/5
[Authorize(Roles = "A")]
public ActionResult Delete(int id = 0)
{
Teachers teachers = db.Teachers.Find(id);
if (teachers == null)
{
return HttpNotFound();
}
return View(teachers);
}
//
// POST: /Teachers/Delete/5
[Authorize(Roles = "A")]
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
Teachers teachers = db.Teachers.Find(id);
db.Teachers.Remove(teachers);
db.SaveChanges();
return RedirectToAction("Index");
}
When you pass an empty string into Html.DropDownList() it looks for a list of items to populate the dropdownlist from the first parameter in the ViewData collection. However, there is already an item in that collection that is of type Int32.
This is one of the many confusing scenarios that happen when you use Html.DropDownList() rather than using a strongly typed model and Html.DropDownListFor()
I suggest you do this:
#Html.DropDownListFor(x => x.DepartmentID, Model.Departments)
You will need to populate your model with a Departments object that is a list of Departments

Resources