Problem with foreach loop ASP.Net core MVC - c#

Previous post
It is with reference to the previous post above.
Is there something wrong with the foreach loop or Action ManageCategories?
The page loads normally, but after removing the category, it cannot pass through the foreach loop
View:
#model collector_forum.Models.Category.CategoryIndexModel
#{
ViewData["Title"] = "Categories";
}
<div class="container body-content">
<div class="row sectionHeader">
<div class="sectionHeading">Browse Categories</div>
<div class="sectionDescription">
<p>Welcome to <strong>Collectors Forum community</strong>. Posts are categorized by their theme</p>
<p>
Please read the Forum Guidelines before creating a new post.
#if (Context.User.Identity.IsAuthenticated)
{
<span>
You must be a
<a asp-area="Identity" asp-page="/Account/Register">registered member</a>
to create a new post
</span>
}
</p>
</div>
</div>
<div class="row" id="categoryIndexContent">
<table class="table table-hover" id="categoryIndexTable">
<tbody>
#foreach (var category in Model.CategoryList)
{
<tr>
<td>
#*<div class="forumLogo" style="background-image: url(#category.ImageUrl);"></div>*#
<div class="categoryData">
<div class="categoryTitle">
<a asp-controller="Forum" asp-action="Topic" asp-route-id="#category.Id">#category.Name</a>
</div>
<div class="categorySubTitle">
#if (category.HasRecentPost)
{
<div class="hasRecentPost">Hot</div>
}
</div>
</div>
</td>
<td>
<div class="categoryPostCount">
#category.NumberOfPosts Posts
</div>
<div class="categoryMemberCount">
#category.NumberOfUsers Users
</div>
</td>
<td>
<div class="categoryDescription">
#category.Description
</div>
</td>
</tr>
}
</tbody>
</table>
</div>
</div>
This is the error
And here code of my Delete Action:
public IActionResult Delete(int id)
{
var category = _categoryService.GetById(id);
if (category == null)
{
ViewBag.ErrorMessage = $"Category with ID = {id} cannot be found";
return View("NotFound");
}
else
{
var result = _categoryService.Delete(id);
if (result.IsCompletedSuccessfully)
{
return RedirectToAction("ManageCategories");
}
return View("ManageCategories");
}
}
What went wrong?
ManageCategories Action:
public IActionResult ManageCategories()
{
var categories = _categoryService.GetAll()
.Select(category => new CategoryListingModel
{
Id = category.Id,
Name = category.Title,
Description = category.Description
});
var model = new CategoryIndexModel
{
CategoryList = categories
};
return View(model);
}
After debugging method Delete seems doesn't work = > image
Delete Action
public async Task Delete(int id)
{
var category = GetById(id);
_context.Remove(category);
await _context.SaveChangesAsync();
}

You may change
return View("ManageCategories");
to
return RedirectToAction("ManageCategories");

Related

How to let the user create a new category directly from a site?

I have created a project in which you can upload, download, delete, and preview files based (and displayed) on categories. How can I enable the user to create and link a new category within the site? I am very new to this and don't know how to. Here is my index page:
#model IEnumerable<Proiect2PracticaClona.Models.Files>
<center>
<h1>Suntem fericiti</h1>
<h2>Ca ne merge proiectu</h2>
<hr />
#if (User.IsInRole("Admin"))
{
<form methord="post" enctype="multipart/form-data" asp-controller="Home" asp-action="Index">
<input type="file" name="fifile" />
<div>Alegeti categoria de skill pentru care doriti sa uploadati</div>
<div>#Html.RadioButton("category", "incepator", "all")<label>Incepatori</label></div>
<div>#Html.RadioButton("category", "intermediar", "all")<label>Intermediari</label></div>
<div>#Html.RadioButton("category", "admin", "all")<label>Admini</label></div>
<input type="submit" value="Uploadati" />
<hr />
</form>}
<div>
File Name:<input id="search" onkeyup="search()" placeholder="cauta"/>
</div>
<table class="table">
<tr>
<th>Alegeti Categoria:</th>
<th class="active">#Html.ActionLink("Incepator", "Index", "Home", new { id = "incepator" })</th>
<th class="active">#Html.ActionLink("Intermediar", "Index", "Home", new { id = "intermediar" })</th>
<th class="active">#Html.ActionLink("Admin", "Index", "Home", new { id = "admin" })</th>
</tr>
<tr>
<th>File Name</th>
</tr>
#foreach (var item in Model)
{
<tr>
<td>#item.info</td>
<td>Download</td>
#if (User.IsInRole("Admin"))
{
<td>Delete</td>
}
</tr>
}
</table>
#section scripts
{
<script>
function search() {
$("tr").each(function (index, value) {
if (index > 0 && !$(this).find("td")[0].innerText.toLowerCase().includes($("#search").val().toLowerCase())) {
$(this).attr("hidden", true);
} else {
$(this).removeAttr("hidden");
}
console.log(value);
})
}
</script>
}
</center>
Controller Action:
[HttpPost]
public async Task<IActionResult> Index(IFormFile fifile, string category)
{
Category.category.Add(category);
var fisave = Path.Combine(_iweb.WebRootPath, "Uploads", fifile.FileName);
var stream = new FileStream(fisave, FileMode.Create);
await fifile.CopyToAsync(stream);
Files fileModel=new Files()
{
info = fifile.FileName,
category = category
};
_fileRepository.AddFile(fileModel);
stream.Close();
files = _fileRepository.GetFileList();
return RedirectToAction("Index",files);
}

The model item passed into the dictionary is of type 'System.Data.Entity.DynamicProxies.Tbl_Categorys

I have problem in my website. I return one value from controller to view but it isn't work and show this message :
The model item passed into the dictionary is of type
'System.Data.Entity.DynamicProxies.Tbl_Categorys_26FAF8D301E15B0158E967BAE1EAE74FFE13A2BB70503E270542F343CFB2ADAB',
but this dictionary requires a model item of type
'RanaShop.Models.Domain.Tbl_User
public ActionResult Product(int id)
{
var q = (from a in db.Tbl_Categorys
where a.ID.Equals(id) && (a.ExitCount > 0)
select a).SingleOrDefault();
if (q == null)
{
ViewBag.Message = "No product to show";
ViewBag.Class = "alert alert-danger";
ViewBag.Style = "width: 40%;padding:15px;background-color:rgb(248,215,218);border
radius:5px;text-align:right;top:20px;margin:0 auto";
return View();
}
return View(q);
}
view
#model RanaShop.Models.Domain.Tbl_Categorys
#{
ViewBag.Title = #Model.Title;
Layout = "~/Views/Shared/_Layout.cshtml";
RanaShop.Models.Domain.ranaDBEntities db = new RanaShop.Models.Domain.ranaDBEntities();
}
<div style="#ViewBag.Style" class="#ViewBag.Class">
#if (ViewBag.Style != null)
{
<button type="button" class="close" data-dismiss="alert" style="float:left">×</button>
}
#Html.Raw(ViewBag.Message)
</div>
#{
if (Model != null)
{
<div class="container padding-top-10 ">
<div class="col-md-7 col-sm-7 col-xs-12">
<div class="well">
<strong>
#Model.Title
</strong>
<p class="help-block ">
#Model.City
</p>
</div>
<span class="line-height-30 " style="font-family:'B Titr'">
#Html.Raw(Model.Text)
</span>
<hr />
<div class="shop-item-price padding-bottom-10 ">
<span>
<strong>
Price : #Model.Price Dollor
</strong>
</span>
</div>
</div>
</div>
}
else
{
<p>
No product to show
</p>
}
}
}
How can I fix it ?
I fix my problem and problem it on the layout .
I should not give the model in layout to Tbl_User . I must give it to Tbl_Category and now its ok.

could not update in session data in asp.net core 2.2

I want to update session data (List type)using a controller. but it's not working.it gives me an error.
Here is my code:
[HttpGet]
public IActionResult Cart()
{
List<Spray> spray = HttpContext.Session.Get<List<Spray>>("spray");
if (spray == null)
{
spray = new List<Spray>();
}
return View(spray);
}
[HttpPost]
public IActionResult Cart(int id)
{
List<Spray> spray = HttpContext.Session.Get<List<Spray>>("spray");
for (int i = 0; i < 10; i++)
{
if (spray[i].Id.Equals(id))
{
spray[i].Quantity++;
}
}
return RedirectToAction(nameof(Cart));
}
View
#using HuddsonBay.Models
#model List<Spray>
#{
ViewData["Title"] = "Cart";
}
<h1>Your Cart</h1>
<br />
<div class="row">
<table class="table table-bordered">
<thead>
<tr>
<th>Image</th>
<th>Name</th>
<th>Price</th>
<th>Product Type</th>
<th>Color</th>
<th></th>
</tr>
</thead>
<tbody>
#foreach (var item in Model)
{
<tr>
<td>
<img src="~/#item.Image" width="200px" height="150px" />
</td>
<td>#item.Name</td>
<td>#item.Price</td>
<td>#item.Quantity</td>
<td>
<partial name="_QuantityPartial" model="#item.Id" />
</td>
<td>#item.ProductColor</td>
<td>
<a asp-area="Customer" asp-action="Remove" asp-controller="SprayShow" asp-route-id="#item.Id" class="btn btn-danger">
<i class="fas fa-trash"></i>
</a>
</td>
</tr>
}
</tbody>
</table>
</div>
<div class="row">
<div class="col-6">
<a asp-action="Index" asp-controller="SprayShow" class="btn btn-primary">Back To Home</a>
</div>
<div class="col-6 text-right">
<h3>Total Amount</h3>
<h3>Grand Total : #Model.Sum(c => c.Price)</h3>
<a asp-area="Customer" asp-action="Checkout" asp-controller="Order" class="btn btn-info">Process To CheckOut</a>
</div>
</div>
_QuantityPartial.cshtml
#model int
<form method="post">
<td style="width:150px">
<div class="btn-group">
#*<a asp-action="Index" class="btn btn-danger" asp-route-id="#Model">Add</a>*#
<input type="submit" asp-action="Cart" asp-route-id="#Model" value="+" />
</div>
</td>
</form>
and here is my output:
and when I click "+" button, I found an error:
will be increment or decrement using a controller. How I will solve this.
I am beginner, please help anyone.
The List<Spray> spray in your post method is a new Instance of the list in your session, after change the propertity's value, you need to store the new list again in session. Otherwise it is still the orignally one.
A test example based on your codes:
View:
#model List<Spray>
#{
ViewData["Title"] = "Cart";
}
<h1>Your Cart</h1>
<br />
<div class="row">
<table class="table table-bordered">
<thead>
<tr>
<th>Name</th>
<th>Price</th>
<th>Quantity</th>
<th></th>
<th>Color</th>
<th></th>
</tr>
</thead>
<tbody>
#foreach (var item in Model)
{
<tr>
<td>#item.Name</td>
<td>#item.Price</td>
<td>#item.Quantity</td>
<td>
<partial name="_QuantityPartial" model="#item.Id" />
</td>
<td>#item.ProductColor</td>
<td>
<a asp-area="Customer" asp-action="Remove" asp-controller="SprayShow" asp-route-id="#item.Id" class="btn btn-danger">
<i class="fas fa-trash"></i>
</a>
</td>
</tr>
}
</tbody>
</table>
</div>
<div class="row">
<div class="col-6">
<a asp-action="Index" asp-controller="SprayShow" class="btn btn-primary">Back To Home</a>
</div>
<div class="col-6 text-right">
<h3>Total Amount</h3>
<h3>Grand Total : #Model.Sum(c => c.Price)</h3>
<a asp-area="Customer" asp-action="Checkout" asp-controller="Order" class="btn btn-info">Process To CheckOut</a>
</div>
</div>
Controller:
[HttpGet]
public IActionResult Cart()
{
var value = HttpContext.Session.GetString("spray");
var spray = JsonConvert.DeserializeObject<List<Spray>>(value);
if (spray == null)
{
spray = new List<Spray>();
}
return View(spray);
}
[HttpPost]
public IActionResult Cart(int id)
{
var value = HttpContext.Session.GetString("spray");
var spray = JsonConvert.DeserializeObject<List<Spray>>(value);
for (int i = 0; i < spray.Count; i++)
{
if (spray[i].Id.Equals(id))
{
spray[i].Quantity++;
}
}
HttpContext.Session.SetString("spray", JsonConvert.SerializeObject(spray));
return RedirectToAction(nameof(Cart));
}
Result:
Your problem maybe not be your Session. Is about the number of the items from the List<Spray> spray. The exception is about the length of the spray list. Your for goes always up to 10 and is possible that you have less items in your list and that is why you have that Out of Range exception.
You have hardcoded 10 in your for loop. If u have less than 10 items in your list it will overflow.
Change to this (or use a foreach)
[HttpPost]
public IActionResult Cart(int id)
{
List<Spray> spray = HttpContext.Session.Get<List<Spray>>("spray");
for (int i = 0; i < spray.Count; i++)
{
if (spray[i].Id.Equals(id))
{
spray[i].Quantity++;
}
}
return RedirectToAction(nameof(Cart));
}
Another option is to skip the loop and do like this instead
public IActionResult Cart(int id)
{
List<Spray> spray = HttpContext.Session.Get<List<Spray>>("spray");
var item = spray.SingleOrDefault(x => x.Id.Equals(id));
if(item != null)
{
item.Quantity++;
}
return RedirectToAction(nameof(Cart));
}

could not update the value of session data in asp.net core

I want to update quantity data, for that, I used a controller, I debugged and I looked, in HTTPpost, the value of quantity value successfully update.but when debug pointer goes to HTTPget method then it not updated and again I found the old quantity value.
for these outputs my code is:
[HttpGet]
public IActionResult Cart()
{
List<Spray> spray = HttpContext.Session.Get<List<Spray>>("spray");
if (spray == null)
{
spray = new List<Spray>();
HttpContext.Session.Set("spray", spray);
}
return View(spray);
}
[HttpPost]
public IActionResult Cart(int id)
{
List<Spray> spray = HttpContext.Session.Get<List<Spray>>("spray");
var item = spray.SingleOrDefault(x => x.Id.Equals(id));
if (item != null)
{
item.Quantity++;
}
return RedirectToAction(nameof(Cart));
}
here I added an old quantity of my session data. this code is:
[HttpGet]
public ActionResult Details(int? id)
{
if (id == null)
{
return NotFound();
}
var product = _db.Spray.Include(c => c.ProductTypes).FirstOrDefault(c => c.Id == id);
if (product == null)
{
return NotFound();
}
return View(product);
}
[HttpPost]
[ActionName("Details")]
public ActionResult ProductDetails(int id,Spray s)
{
var r = _db.Spray.Include(c => c.ProductTypes).FirstOrDefault(c => c.Id == id);
r.Quantity = s.Quantity;
//List<Spray> spray = new List<Spray>();
if (Utility.SessionExtensions.Get<List<Spray>>(HttpContext.Session, "spray") == null)
{
List<Spray> spray = new List<Spray>();
spray.Add(r);
r.Quantity = s.Quantity;
Utility.SessionExtensions.Set(HttpContext.Session, "spray", spray);
}
else
{
List<Spray> spray = Utility.SessionExtensions.Get<List<Spray>>(HttpContext.Session, "spray");
int index = isExist(id);
if (index != -1)
{
spray[index].Quantity++;
}
else
{
var m = _db.Spray.Include(c => c.ProductTypes).FirstOrDefault(c => c.Id == id);
m.Quantity = s.Quantity;
spray.Add(m);
}
Utility.SessionExtensions.Set(HttpContext.Session, "spray", spray);
}
return RedirectToAction("Index");
}
private int isExist(int id)
{
List<Spray> spray = Utility.SessionExtensions.Get<List<Spray>>(HttpContext.Session, "spray");
var r = _db.Spray.Include(c => c.ProductTypes).FirstOrDefault(c => c.Id == id);
for (int i = 0; i < spray.Count; i++)
{
if (spray[i].Id.Equals(id))
{
return i;
}
}
return -1;
}
Details.cshtml
<form method="post" asp-action="" enctype="multipart/form-data">
<div class="row">
<div class="col-1">
<img src="~/#Model.Image" style="width:100%" onclick="myFunction(this);">
</br></br>
<img src="~/#Model.Image1" style="width:100%" onclick="myFunction(this);">
</div>
<div class="col-4 container">
<span onclick="this.parentElement.style.display='none'" class="closebtn">×</span>
<img id="expandedImg" style="width:100%">
<div id="imgtext"></div>
</div>
<div class="col-4">
<input type="hidden" asp-for="Id" />
<h2>#Model.Name</h2>
<p><b>#Model.Description</b></p>
<h4>#Model.Price $</h4>
<small class="text-danger">Clearence</small>
</br>
<hr>
<h4>Size:#Model.Size</h4>
<h6>Product Color:#Model.ProductColor</h6>
</br></br></br></br>
<script src="https://code.jquery.com/jquery-2.2.4.min.js" integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script>
<div class="col-sm-12 col-xs-12 spinner-block">
<div class="number-spinner">
<div class="input-group number-spinner">
<b class="mr-4"> <label asp-for="#Model.Quantity"></label></b></br>
<span class="input-group-btn">
<button type="button" class="btn btn-default btn-number btncartsniper" data-type="minus" data-dir="dwn"><span class="fa fa-minus fa-sm"></span></button>
</span>
<input asp-for="#Model.Quantity" class="form-control input-number Snippper_qty" value="0" type="number">
<span class="input-group-btn">
<button type="button" class="btn btn-default btn-number btncartsniper" data-type="plus" data-dir="up"><span class="fa fa-plus fa-sm"></span></button>
</span>
</div>
</div>
</div>
<input type="submit" value="Add To Cart" class="btn btn-dark form-control" />
#if (Model.Quantity > 0)
{
<h3>This Product Is InStock</h3>
}
else
{
<h1>Not InStock</h1>
}
</div>
</div>
<div>
<a asp-action="Index" class="btn btn-dark">Back To List</a>
</div>
</form>
Cart.cshtml
#using HuddsonBay.Models
#model List<Spray>
#{
ViewData["Title"] = "Cart";
}
<h1>Your Cart</h1>
<br />
<div class="row">
<table class="table table-bordered">
<thead>
<tr>
<th>Image</th>
<th>Name</th>
<th>Price</th>
<th>Quantity</th>
<th>Color</th>
<th></th>
</tr>
</thead>
<tbody>
#foreach (var item in Model)
{
<tr>
<td>
<img src="~/#item.Image" width="200px" height="150px" />
</td>
<td>#item.Name</td>
<td>#item.Price</td>
<td>#item.Quantity</td>
<td>
<partial name="_QuantityPartial" model="#item.Id" />
</td>
<td>#item.ProductColor</td>
<td>
<a asp-area="Customer" asp-action="Remove" asp-controller="SprayShow" asp-route-id="#item.Id" class="btn btn-danger">
<i class="fas fa-trash"></i>
</a>
</td>
</tr>
}
</tbody>
</table>
</div>
<div class="row">
<div class="col-6">
<a asp-action="Index" asp-controller="SprayShow" class="btn btn-primary">Back To Home</a>
</div>
<div class="col-6 text-right">
<h3>Total Amount</h3>
<h3>Grand Total : #Model.Sum(c => c.Price)</h3>
<a asp-area="Customer" asp-action="Checkout" asp-controller="Order" class="btn btn-info">Process To CheckOut</a>
</div>
</div>
my output is:
when I click the "+" button, then it responds goes to the cart "httpPost" here quantity value increase but when debug pointer goes cart "HTTPget" then I found the old value of the quantity.for that similarly, I can not find the updated quantity value of my output. I want to increase my quantity value.
I am a beginner. please help anyone.
The session data in ASP.NET Core is serialized, not as actual objects. This means when you get an object and modify it nothing is stored, since there’s no link with the serialized data in session store. You have to store the modified list back into the session after modifying anything in it.
See documentation
[HttpPost]
public IActionResult Cart(int id)
{
List<Spray> spray = HttpContext.Session.Get<List<Spray>>("spray");
var item = spray.SingleOrDefault(x => x.Id.Equals(id));
if (item != null)
{
item.Quantity++;
HttpContext.Session.Set("spray", spray); //it's must add for a set new session data
}
return RedirectToAction(nameof(Cart));
}

Is it possible to populate a viewmodel and form error messages via ViewData[]?

(Update: I found out that the Model in a view is read-only, so my initial idea of populating the model properties via ViewData is not possible. Any other workaround?)
I have some CRUD-functionality as partial views. If the submitted form is invalid, I'm not able to redisplay the form with the incomplete data. Can it be done?
In my main view for a project (Details.cshtml), I have this to display the notes of that project:
<partial name="_notes" model="#new ProjectNotesViewModel
{
ProjectId = Model.Id,
Notes = Model.Notes,
ProjectNoteId = Model.ProjectNoteId,
ProjectNoteAction = Model.ProjectNoteAction,
ProjectCompleted = Model.Completed
}" />
This is the controller method "Details()" for a project:
public async Task<IActionResult> Details(int? id, int n, string a)
// n = ProjectNoteId, a = ProjectNoteAction ("c"=create, "e"=edit, "d"=delete, "v"=view (details))
{
Project project = await db.Projects
.Include(n => n.Notes)
.Include(p => p.ParentProject)
.Include(c => c.ChildProjects)
.ThenInclude(o => o.ProjectOwner)
.Include(o => o.ProjectOwner)
.FirstOrDefaultAsync(m => m.Id == id);
if (project == null) { return NotFound(); }
ProjectViewModel vm = auto.Map<ProjectViewModel>(project);
vm.ProjectNoteId = n;
vm.ProjectNoteAction = a;
vm.ProjectCompletedAction = a;
return View(vm);
}
The partial "_notes.cshtml" equates to Index.cshtml for project notes.
In _notes.cshtml, I have this:
#foreach (ProjectNoteViewModel note in Model.Notes)
{
if (note.Id == Model.ProjectNoteId)
{
// Then we want to do something to this particular note:
<tr class="alert-black-50">
<td class="align-text-top text-light rounded-left" width="1">
<span class="fas fa-clipboard fa-fw"></span>
</td>
<td class="pb-2 pr-2 text-light rounded-right">
#if (Model.ProjectNoteAction == "v") // v = view (details)
{
// View the details of the note
<partial name="_noteDetails" model="#note" />
}
#if (Model.ProjectNoteAction == "e") // = edit
{
// Edit the note
<partial name="_noteEdit" model="#note" />
}
#if (Model.ProjectNoteAction == "d") // = delete
{
// Delete the note
<partial name="_noteDelete" model="#note" />
}
</td>
</tr>
}
else
{
// Just display the date and title:
<tr>
<td colspan="2">
<a asp-action="Details"
asp-route-n="#note.Id"
asp-route-a="v">
<span class="fas fa-clipboard fa-fw"></span>
#note.Created – #note.Title
</a>
</td>
</tr>
}
}
If we for instance want to create a new project note, this is where that happens ("_noteCreate.cshtml"):
#model Projects.Models.ProjectNoteViewModel
<form asp-action="CreateNote">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<input type="hidden" asp-for="ProjectId" />
<div class="form-group">
<label asp-for="Title" class="control-label"></label>
<input asp-for="Title" class="form-control" />
<span asp-validation-for="Title" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Contents" class="control-label"></label>
<textarea asp-for="Contents" class="form-control"></textarea>
<span asp-validation-for="Contents" class="text-danger"></span>
</div>
<div class="form-group">
<a asp-action="Details" asp-route-id="#Model.ProjectId" class="btn btn-primary btn-split">
<span class="fas fas-split fa-chevron-circle-left fa-fw"></span> Cancel
</a>
<button type="submit" class="btn btn-success btn-split">
<span class="fas fas-split fa-save fa-fw"></span> Save
</button>
</div>
</form>
The "CreateNote" controller method:
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> CreateNote(int projectId, [Bind("Title,Contents")] ProjectNote note)
{
Project project = await db.Projects.Where(p => p.Id == projectId).FirstOrDefaultAsync();
if (project == null)
{
return NotFound();
}
if (project.Completed)
{
// Prevent new notes from being created on completed projects.
return RedirectToAction("Details", new { id = project.Id });
}
note.ProjectId = projectId;
note.Created = DateTime.Now;
if (ModelState.IsValid)
{
db.Add(note);
await db.SaveChangesAsync();
return RedirectToAction("Details", new { id = projectId });
}
ProjectNoteViewModel vm = auto.Map<ProjectNoteViewModel>(note);
vm.Project = auto.Map<ProjectViewModel>(project);
ViewData["draft"] = vm; // Can I somehow populate the form from ViewData?
return RedirectToAction("Details", new { id = projectId });
//return View(vm); // I can't do this, because there is no "CreateNote.cshtml"
}
The last five lines of the above method is what I can't figure out. In a normal CRUD-situation, where the name of the controller method would be Create(), and we had a view called "Create.cshtml", this would be the way to do it:
ProjectNoteViewModel vm = auto.Map<ProjectNoteViewModel>(note);
vm.Project = auto.Map<ProjectViewModel>(project);
return View(vm);
But I can't return View(), because there is no view called "CreateNote". I need to bring the user back to "Projects/Details/5?a=c", where a=c (action=create) indicates that we are creating a new note on that project. And I want to bring the incomplete form data with me.

Categories

Resources