I am trying to figure out how to insert multiple rows of data using a viewmodel with a single postback. I am using EF and code first approach. The model I an having trouble with has one to many relationship. What I would like to do is that whenever a Person is created, multiple Tasks should be saved to the database along with a Person (different tables).
So far, I am only able to save the very first Task and not all.
My models are:
public class Person
{
public Person()
{
this.Tasks = new List<Task>();
}
[Key]
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Task> Tasks { get; set; }
}
public class Task
{
[key]
public int ID { get; set; }
public int PersonID { get; set; }
public string Task { get; set; }
public virtual Person Person { get; set; }
}
ViewModel:
Public class PersonData
{
public Person Person { get; set; }
public Task Task { get; set; }
}
View:
#model Project.ViewModels.PersonData
#using (Html.BeginForm()) {
#Html.ValidationSummary(true)
<fieldset>
<legend>Event</legend>
<div>
#Html.TextBoxFor(model => model.Event.Name)
</div>
<div id="taskdiv">
</div>
#*dynamically generated textboxes here*#
<div>
#Html.ActionLink("Back to List", "Index")
</div>
</fieldset>
<script type="text/javascript">
// took few codes out here, but whats happening here is based on int value textboxes for Task appears
for (var i = 1; i <= $count; i++) {
$('#taskdiv').append('<div><label>Task #' + i + ' </label>#Html.TextBoxFor(model => model.Task.Task)</div>');
}
Controller:
public class PersonController : Controller
{
private Context db = new DBContext();
public ActionResult Create()
{
return View();
}
[HttpPost]
public ActionResult Create(ViewModels.PersonData personData)
{
if (ModelState.IsValid)
{
db.Person.Add(eventData.Person);
db.Tasks.Add(eventData.Task);
db.SaveChanges();
return RedirectToAction("Index");
}
return View();
}
At least I know that tasks are being passed. I tried few things like
foreach (char c in personData.Task.Task.AsEnumerable)
{ count += 1}
and received the correct amount of number. I thought using ViewModels are relatively common, but I didn't see what I would like to do in any tutorials or forums....
My goal is to be able to save multiple tasks per person with one postback. Any advice will be appreciated!
In your ViewModel you only have one Task, should be a list of tasks right?
When I do stuff like this I usually save the Person object first then add tasks to it using ajax.
The Method could look something like this.
public void SaveTasks(List<Task> tasks){
//important that tasks already have personId --set on client side
foreach(var task in Tasks){
//do some validation
db.Tasks.Add(task);
}
db.SaveChanges();
}
Related
I'm new at this and trying to figure out what happen and think this is the best place to ask. Well when I select the project and press the add button the property [Bind""]Model came null but why?
This is my View:
#model PortfolioDetailsVM
<form asp-controller="Portfolio" asp-action="AddProject" method="POST">
<div class="form-group">
<div class="input-group mb-3">
<select asp-for="PortfolioProjects.ProjectId" class="custom-select form-control">
<option disabled selected value="#null">Choose...</option>
#foreach (var item in Model.Projects)
{
<option value="#item.ProjectID">#item.Title</option>
}
</select>
The var PortfolioVM came null with any data.
And this is my Controller and my View Model:
namespace PEG.Models
{
public class PortfolioDetailsVM
{
public PortfolioDetailsVM()
{
Portfolios = new Portfolio();
PortfolioProjects = new PortfolioProject();
}
public Portfolio Portfolio;
public PortfolioProject PortfolioProjects;
public IEnumerable<Project> Projects;
}
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> AddProject([Bind("PortfolioProject")]PortfolioDetailsVM PortfolioVM) //<----Null
{
var addproject = PortfolioVM.PortfolioProjects;
if (ModelState.IsValid)
{
try
{
context.Update(addproject);
await context.SaveChangesAsync();
return RedirectToAction("Details", "Portfolio" + PortfolioVM.PortfolioProjects.PortfolioId);
}
catch (DbUpdateException)
{
ModelState.AddModelError("", "Unable to save changes. " +
"Try again, and if the problem persists, " +
"see your system administrator.");
}
}
return RedirectToAction("Index", "Portfolio");
}
This the other model and Details Method:
// GET: Portfolio/Details/5
public async Task<ActionResult> Details(int id)
{
PortfolioDetailsVM PortfolioVM = new PortfolioDetailsVM
{
Projects = await context.Project.Include(x => x.Task).ToListAsync(),
Portfolios = await context.Portfolio.SingleOrDefaultAsync(x => x.PortfolioID == id)
};
return View(PortfolioVM);
}
namespace PEG.Models
{
public partial class PortfolioProject
{
[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
[Column(TypeName = "datetime2")]
public DateTime? CreatedDate { get; set; }
//RelationsId
[Key]
[Column(Order = 0)]
public int PortfolioId { get; set; }
[Key]
[Column(Order = 1)]
public string ProjectId { get; set; }
//Relations
[ForeignKey("PortfolioId")]
public virtual Portfolio Portfolio { get; set; }
[ForeignKey("ProjectId")]
public virtual Project Project { get; set; }
}
}
Bind to properties
First off, you have defined fields in your ViewModels. They can be read in your View, but for Model Binding to work you need to declare them as Properties, with a get and set accessor:
public class PortfolioDetailsVM
{
//...
public Portfolio Portfolio { get; set; }
public PortfolioProject PortfolioProject { get; set; }
public IEnumerable<Project> Projects { get; set; }
}
This should make your binding code work.
Better binding models
Second, you are using Model Binding in a slightly incorrect way. Try not to bind directly to your data models (e.g. the type of PortfolioProject). The model you're binding to shouldn't contain any reference to data model types.
Instead, I usually only declare what I really need in the model I'm binding to, so that I won't ever have to use that ol' Bind attribute in the first place. A simple example for your case:
public class DetailsAddProjectVM
{
public string SelectedProjectId { get; set; }
}
With a corresponding form:
#model PortfolioDetailsVM
<select asp-for="SelectedProjectId" class="custom-select form-control">
...
</select>
which posts to
public async Task<IActionResult> AddProject(DetailsAddProjectVM bindingModel)
{
//look ma, no [Bind]!
var projectid = bindingModel.SelectedProjectId;
}
Of course, for the corresponding form to render, you'd also have to declare a SelectedProjectId property in your original PortfolioDetailsVM.
As you can see, you don't have to bind to your original View Model at all.
I am new to ASP.NET MVC and trying to list some companies, then all contacts under that each company. I think I am getting close to make it work, so please help if you can.
The model for table and field name:
namespace ERP.Models
{
[Table("ERP_Company")]
public class ERP_Company
{
[Key]
public int CompanyID { get; set; }
public string Name { get; set; }
}
[Table("ERP_CompanyContact")]
public class ERP_Contact
{
[Key]
public int ContactID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int CompanyID { get; set; }
}
}
These are the methods getting the Company and Contact list:
namespace ERP.Models
{
public class Method1
{
public ERPEntities db = new ERPEntities();
public List<ERP_Company> getCompanyList()
{
List<ERP_Company> companyList = (
from c in db.ERP_Company
where c.Name.Contains("Network")
select c).Take(10).ToList();
return companyList;
}
// This below method needs to get the passing CompanyID from getCompanyList for filtering.
public List<ERP_Contact> getContactList()
{
List<ERP_Contact> contactList = (
from cc in db.ERP_CompanyContact
select cc).Take(50).ToList();
return contactList;
}
/* Tried this below, but not work for the Controller, maybe I am doing wrong.
public List<ERP_Contact> getContactList(int CompanyID)
{
List<ERP_Contact> contactList = (
from cc in db.ERP_CompanyContact
where cc.CompanyID == CompanyID
select cc).Take(50).ToList();
return contactList;
}
*/
}
}
Use ViewModel (suggested from other post), combine both models:
namespace ERP.Models
{
public class ViewModelDemoVM
{
public List<ERP_Company> allCompanies { get; set; }
public List<ERP_Contact> allContacts { get; set; }
}
}
This code in the Controller:
Method1 _repository = new Method1();
public ActionResult ViewModelDemo()
{
ViewModelDemoVM vm = new ViewModelDemoVM();
vm.allCompanies = _repository.getCompanyList();
vm.allContacts = _repository.getContactList();
return View(vm);
}
Lastly, the view code:
#model ERP.Models.ViewModelDemoVM
#{
ViewBag.Title = "ViewModelDemo";
}
<h2>ViewModelDemo</h2>
<ul>
#foreach (var company in Model.allCompanies)
{
<li>#company.CompanyID | #company.Name</li>
<ul>
<!-- HERE is I want to put the filtering... foreach contact WHERE CompanyID = Model.allCompanies.CompanyID-->
#foreach (var contact in Model.allContacts)
{
<li>#contact.ContactID | #contact.FirstName</li>
}
</ul>
}
</ul>
How can I filter the contacts (2nd loop) based on the #company.CompanyID? Sample code would be appreciated.
Thanks in advance.
you can apply where clause in second loop. try below code. i hope this helps
#model ERP.Models.ViewModelDemoVM
#{
ViewBag.Title = "ViewModelDemo";
}
<h2>ViewModelDemo</h2>
<ul>
#foreach (var company in Model.allCompanies)
{
<li>#company.CompanyID | #company.Name</li>
<ul>
<!-- HERE is I want to put the filtering... foreach contact WHERE CompanyID = Model.allCompanies.CompanyID-->
#foreach (var contact in Model.allContacts.Where(x=>x.CompanyId ==company.CompanyID)
{
<li>#contact.ContactID | #contact.FirstName</li>
}
</ul>
}
</ul>
You should create a view model with nested structure and use that. Remember, view models are specific to the view. So build it as your view needs it.
public class CompanyVm
{
public string Name { set; get; }
public IEnumerable<ContactVm> Contacts { set;get;}
}
public class ContactVm
{
public string Name { set; get; }
}
public class ViewModelDemoVM
{
public List<CompanyVm> Companies { set; get; }
}
Your Contact table/entity already has a foriegn key/navigational property to Company entity/table. So all you have to do is, get the companies and it's corresponding customers,map it to our view model and use it in the view.
Add a collection type to Company entity class to access it's contacts.
public class ERP_Company
{
[Key]
public int CompanyID { get; set; }
public string Name { get; set; }
public ICollection<Contact> Contacts { set; get; }
}
Now in your action method, you can get the data
public IActionResult ViewModelDemo()
{
var vm = new ViewModelDemoVM();
vm.Companies = db.Companies
.Select(a => new CompanyVm { Name = a.Name,
Contacts = a.Contacts
.Select(c => new ContactVm
{ Name = c.Name })}
).ToList();
return View(vm);
}
Now in your view, just loop through the company and for each company, loop through it's contacts
#model ViewModelDemoVM
#foreach(var company in Model.Companies)
{
<h3>#company.Name</h3>
<h5>Contacts</h5>
#foreach(var contact in company.Contacts)
{
<p>#contact.Name</p>
}
}
Some notes
Create view models as needed by view
Do not mix entity classes (used by ORM) with view models
Keep less logic/C# code in views
I used class generic class names (Contact instead ERP_Contact) and property names. When you use the above code, make the needed changes to use your existing names if needed.
I'm having issues updating a Boolean from 'false' to 'true' in my database.
I am using Asp.Net MVC5 in Visual Studio 2017, and have created a database (using entity framework code-first) to contain two tables - task and steps (one-to-many relationship).
The index.cshtml page is laid out to list all of the tasks along with their relevant steps, and then each step has a 'mark as completed' button beside it to change the 'completed' field in the steps entity from false to true.
Here is my code:
Steps.cs:
public class Steps
{
[Key]
public int Id { get; set; }
public int StepNumber { get; set; }
public string Description { get; set; }
public ToDoTask Task { get; set; }
public bool Completed { get; set; }
public DateTime? CompletedDate { get; set; }
public Client Client { get; set; }
}
Index.cshtml:
#foreach (var step in ViewData["steps"] as Dictionary<Steps, int>)
{
if (step.Value == task.Id)
{
<p>Step Number: #step.Key.StepNumber</p>
<p>Step Description: #step.Key.Description</p>
using (#Html.BeginForm("MarkStepAsCompleted", "Tasks"))
{
<div class="col-md-2" style="display:none">
#Html.TextBox("Id", #step.Key.Id)
</div>
<button type="submit" >Mark As Completed</button>
}
}
}
TasksController.cs:
[HttpPost]
[AllowAnonymous]
public ActionResult MarkStepAsCompleted(FormCollection form)
{
var completedStepId = Convert.ToInt32(form["Id"]);
var completedStep = db.Steps.Where(s => s.Id == completedStepId).FirstOrDefault();
StepMethods.MarkAsCompleted(completedStep);
return Redirect("Index");
}
StepMethods.cs:
public static void MarkAsCompleted(Steps step)
{
var context = new ApplicationDbContext();
var stepid = step.Id;
context.Steps.Find(stepid);
step.Completed=true;
context.SaveChanges();
}
The application runs well with no errors and when I hit the 'Mark As Completed' button, it redirects to the index page as wanted. But when I check the table in Server Explorer, the value in the 'Completed' column still says false.
You are not updating the entity retrieved from the database, but instead the local instance. Change to this:
public static void MarkAsCompleted(Steps step)
{
using (var context = new ApplicationDbContext())
{
step = context.Steps.Find(step.id); //use the retrieved instance
step.Completed = true;
context.SaveChanges();
}
}
Also, always use the using statement when working with ApplicationDbContext (or, in general, any class that implements the IDisposable interface)
I started working with asp.net and I have encountered a problem when I try to edit multiple values from a table. I have a bookmark tables which is connected to another tag table, with an 1 : N relationship. My problem is when I want to edit already existing tags associated with an existing url. I can display them on the page but when I try to post the edited data I don't know how to pick it up in the controller. So far I have managed to send them back as a string but I doubt that is the solution since I have to edit all the data again later. I want to replace the existing values in the Tag table with the edited data. Here are my model and controller code snippets.
Bookmark model:
public int id { get; set; }
public string url { get; set; }
public virtual ICollection<Tag> tags { get; set; }
Tag model:
public int id { get; set; }
public string name { get; set; }
public virtual Bookmark bookmark { get; set; }
public string user { get; set; }
Controller:
public ActionResult Edit(int id)
{
var editBookmark = adc.Bookmarks.Single(x => x.id == id);
var query_where2 = from a in adc.Tags
where a.bookmark.id == id
select a;
BookmarkTag bkTag = new BookmarkTag();
bkTag.bookmark = new List<Bookmark>();
bkTag.bookmark.Add(editBookmark);
bkTag.tag = query_where2.ToList();
return View(bkTag.tag);
}
//
// POST: /SavedBookmark/Edit/5
[HttpPost]
public ActionResult Edit(int id, ICollection<FormCollection> tag)
{
try
{
return View();
}
catch
{
return View();
}
Html code:
#using (Html.BeginForm("edit", "SavedBookmark"))
{
#Html.AntiForgeryToken()
if (Model != null) {
var aa= Model.First();
#Html.TextBox("test2", aa.bookmark.url);
List<BookIT2.Models.Tag> allTags = new List<BookIT2.Models.Tag>();
allTags = Model.ToList();
for (int i = 0; i < allTags.Count; i++)
{
if (!allTags[i].name.IsEmpty())
{
#Html.TextBox(allTags[i].name, allTags[i].name);
#Html.Hidden(allTags[i].id.ToString(), allTags[i].id);
#Html.Hidden(allTags[i].user, allTags[i].user)
#Html.Hidden(allTags[i].bookmark.id.ToString(), allTags[i].bookmark.id.ToString())
}
}
#Html.Label("Additional tag")
#Html.TextBox("additionalTag")
<input type="submit" value="edit" />
}
In short: I can't get any values in the http post ICollection, it's always null.
Here is the updated code:
#using (Html.BeginForm("edit", "SavedBookmark"))
{
#Html.AntiForgeryToken()
if (Model != null)
{
for (int i = 0; i < Model.tag.Count; i++)
{
if (!Model.tag[i].name.IsEmpty()) {
#Html.Hidden(Model.tag[i].id.ToString(), Model.tag[i].id);
#Html.Label("name");
#Html.TextBox(Model.tag[i].name, Model.tag[i].name);
#Html.Hidden(Model.tag[i].bookmark.id.ToString(), Model.tag[i].bookmark.id);
#Html.Hidden(Model.tag[i].user, Model.tag[i].user);
}
}
#Html.TextBox(Model.bookmark.id.ToString(), Model.bookmark.url);
<input type="submit" value="edit" />
}
}
Model class:
public class TestBookmark
{
public Bookmark bookmark{get; set;}
public List<Tag> tag {get; set;}
}
[HttpPost]
public ActionResult Edit(TestBookmark edit)
{}
Don't really understand why you're doing it this way. I would like to suggest you totally different approach.
First:
Create a class with all the fields you want in your view.
Second:
Use this class as the MODEL in your View
Third:
In the controller, in the POST function user your class as the only one parameter of that function.
Can't get all my ViewModel properties back after the postback (After user entered some values on HttpPost)
There are numerous questions here related to losing data or getting nulls after the postback
I tried some of them and played around on my case, I think the scenario is a bit different,
Using a PartialView or Editor Templates(except a list property), Always the returned result properties are null.
In partialView approach always all the properties are null, I think maybe I missed a piece.
In the "custom editor template approach for the type", I'll have just "EnteredNums" List returned. (Maybe because these are what the template have EditorFor for them, but what is the solution here if that's the case?)
Don't know weather if it's important here or not, the application also uses Unity. I don't think it be the problem here.
The HttpGet Passed model is the same as HttpPost : DataVm
The name of the action is also the same : ProcessEnteredData
=================== Controller and action
[HttpPost]
public ActionResult ProcessEnteredData(DataVm vm)
{
if (ModelState.IsValid)
{
foreach (NumType num in vm.EnteredNums)
{
int i1 = num.Score1;
int i2 = num.Score2;
string profTitle = vm.Profile.Title;
Repository.Context.EnteredNums.Add(num);
}
return RedirectToAction("ShowTable");
}
else
{
return View(vm);
}
}
==============
The Partial View of Custom Editor Template are similar :
#model xxxx.NumType
#Html.LabelFor(m => m.TheTitle)
#Html.TextBoxFor(m => m.Score1)
#Html.TextBoxFor(m => m.Score2)
#Html.HiddenFor(m => m.Profile)
// Profile or ProfileId - Just used to see could it bring the property back or not as a test
============
NumType Model
[Key]
public int NumTypeId { get; set; }
[ForeignKey("Profile")]
[Required]
public int ProfileId { get; set; }
public int Score1 { get; set; }
public int Score2 { get; set; }
public int BoxId { get; set; }
public Box Box { get; set; } // something not important here
public virtual Profile Profile { get; set; }
============
The ViewModel
public class DataVm
{
public Profile Profile { get; set; }
public string TheTitle { get; set; }
public List<NumType> EnteredNums { get; set; }
// In the Editor template approach it's the only item with data and others are null
public List<Box> Boxes { get; set; }
}
=========
View for PartialView approach :
#model xxxx.DataVm
#using (Html.BeginForm("ProcessEnteredData", "Profile", FormMethod.Post))
{
#Html.AntiForgeryToken()
Model.EnteredNums = new List<NumType>();
foreach(var box in Model.Boxes)
{
NumType num = new NumType();
num.Profile = Model.Profile;
num.Box = box;
int iCount = Model.EnteredNums.Count;
Model.EnteredNums.Add(num);
#Html.Partial("NumView", Model.EnteredNums[iCount]);
}
<input type="submit" value="Do Process" />
}
===================
View for Editor for approach :
// instead of #Html.Partial :
#Html.EditorFor(m => m.EnteredNums[iCount]);