Delete multiple rows in mvc don't work - model-view-controller

I have class connection
public partial class Connections
{
public System.Guid IdConnection { get; set; }
[Required(ErrorMessage = "Requienter code hereenter code here`red")]
public string Sign { get; set; }
[DataType(DataType.Date)]
[Required(ErrorMessage = "Required")]
public System.DateTime Date { get; set; }
[DataType(DataType.Time)]
[Required(ErrorMessage = "Required")]
public System.DateTime Time { get; set; }
[Required(ErrorMessage = "Required")]
public double Band { get; set; }
[Required(ErrorMessage = "Required")]
public string Modulation { get; set; }
[Required(ErrorMessage = "Required")]
public int RST { get; set; }
public string Comment { get; set; }
[Required(ErrorMessage = "Required")]
public string QLS { get; set; }
}
Method in controller:
[HttpPost]
public ActionResult DeleteMultiple(System.Guid[] deleteInputs)
{
var db= new MainDbContext();
var connections = db.Connections.ToList();
if (deleteInputs == null)
{
}
foreach (var item in deleteInputs)
{
Connections con = db.Connections.Find(item);
db.Connections.Remove(con);
}
db.SaveChanges();
return RedirectToAction("EditDelete", "Connection");
}
and View:
#model IEnumerable<RadioSite.Connections>
#{
ViewBag.Title = "My connections";
}
<table class="table">
<tr>
<th>
Select
</th>
<th>
#Html.DisplayNameFor(model => model.Sign)
</th>
<th>
#Html.DisplayNameFor(model => model.Date)
</th>
<th>
#Html.DisplayNameFor(model => model.Time)
</th>
<th>
#Html.DisplayNameFor(model => model.Band)
</th>
<th>
#Html.DisplayNameFor(model => model.Modulation)
</th>
<th>
#Html.DisplayNameFor(model => model.RST)
</th>
<th>
#Html.DisplayNameFor(model => model.Comment)
</th>
<th>
#Html.DisplayNameFor(model => model.QLS)
</th>
<th colspan="2">
Actions
</th>
</tr>
#foreach (var item in Model)
{
<tr>
<td>
<input type="checkbox" name="deleteInputs" id="deleteInputs" value="#item.IdConnection" />
</td>
<td>
#Html.DisplayFor(modelItem => item.Sign)
</td>
<td>
#Html.ValueFor(modelItem => item.Date, "{0:dd/MM/yyyy}")
</td>
<td>
#Html.ValueFor(modelItem => item.Time, "{0:HH:MM}")
</td>
<td>
#Html.DisplayFor(modelItem => item.Band)
</td>
<td>
#Html.DisplayFor(modelItem => item.Modulation)
</td>
<td>
#Html.DisplayFor(modelItem => item.RST)
</td>
<td>
#Html.DisplayFor(modelItem => item.Comment)
</td>
<td>
#Html.DisplayFor(modelItem => item.QLS)
</td>
<td>
#using (Html.BeginForm("Edit", "Connection", FormMethod.Get))
{
<input type="submit" value="Edit" formaction="/Connection/Edit/#item.IdConnection" />
}
</td>
<td>
#using (Html.BeginForm("DeleteItem", "Connection", FormMethod.Post))
{
<input type="submit" value="Delete" formaction="/Connection/DeleteItem/#item.IdConnection" />
}
</td>
</tr>
}
</table>
#using (Html.BeginForm("DeleteMultiple", "Connection", FormMethod.Post))
{
<input type="submit" value="Delete Selected"/>
}
I selected items in grid (checked checkbox) and in method :public ActionResult DeleteMultiple(System.Guid[] deleteInputs) array deleteInputs always is null.

Your html.beginform pointing to "DeleteMultiple" has nothing being passed to it! You need to either restructure your view to include the inputs in your form or, since this is a delete function, make an ajax call to your controller action and send in the values of the selected check boxes.

Related

ASP.NET Core 3.1 MVC : sending selected checkboxes to controller

I have following list view with checkboxes:
#model IEnumerable<PaketServisAracTakip.Models.Item>
#{
ViewData["Title"] = "Yükleme Yap";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2 class="text-center text-success">#ViewBag.name İsimli Araca Yükleme Yap</h2>
<form asp-controller="Vehicle"
asp-action="LoadItem" method="post">
<br />
<input type="submit" name="submit" value="Oluştur" class="btn btn-primary" />
</form>
<table class="table table-bordered table-striped table-hover">
<thead>
<tr>
<th class="text-center">
Yüklensin mi?
</th>
<th class="text-center">
#Html.DisplayNameFor(model => model.Name)
</th>
<th class="text-center">
#Html.DisplayNameFor(model => model.Price)
</th>
<th class="text-center">
#Html.DisplayNameFor(model => model.Description)
</th>
</tr>
</thead>
<tbody>
#foreach (var item in Model)
{
<tr>
<td>
<div class="form-check">
<label class="form-check-label" for="#item.Id" asp-validation-for="#item.Id"></label>
<input class="form-check-input" name="ids" type="checkbox" value="#item.Id" id="#item.Id">
</div>
</td>
<td>
#Html.DisplayFor(modelItem => item.Name)
</td>
<td>
₺#Html.DisplayFor(modelItem => item.Price)
</td>
<td>
#Html.DisplayFor(modelItem => item.Description)
</td>
</tr>
}
</tbody>
</table>
And my item model with database integrated:
[Table("Items")]
public class Item
{
[Key]
public int Id { get; set; }
[Display(Name = "İsim")]
[Required(ErrorMessage ="{0} alanı boş bırakılamaz.")]
[MaxLength(50, ErrorMessage ="İsim 50 karakteri geçemez.")]
public String Name { get; set; }
[Display(Name = "Fiyat")]
[Required(ErrorMessage = "{0} alanı boş bırakılamaz.")]
[Range(0, Double.MaxValue, ErrorMessage = "Minimum 0 girmelisin.")]
public int Price { get; set; }
[Display(Name = "Açıklama")]
public String Description { get; set; }
}
view
So when button is clicked i want to get checked items in my controller. I tried this but its empty:
[HttpPost]
public ActionResult LoadItem(IEnumerable<Item> model)
{
return RedirectToAction("Index");
}
I also tried int array and FormCollection but didn't work. I think I need some tag helpers but don't know which.
when button is clicked i want to get checked items in my controller. I
tried this but its empty
Please check the code in the View Page, since the table doesn't in the <form> element, when you click the Submit button, the submitted form doesn't contain the related data.
Besides, to submit the model data to the controller using model binding, we should use the #for statement to loop through the entities and use hidden fields to store the related data. Please refer the following sample and change your code:
Model:
[Table("Items")]
public class Item
{
[Key]
public int Id { get; set; }
[Display(Name = "İsim")]
[Required(ErrorMessage = "{0} alanı boş bırakılamaz.")]
[MaxLength(50, ErrorMessage = "İsim 50 karakteri geçemez.")]
public String Name { get; set; }
[Display(Name = "Fiyat")]
[Required(ErrorMessage = "{0} alanı boş bırakılamaz.")]
[Range(0, Double.MaxValue, ErrorMessage = "Minimum 0 girmelisin.")]
public int Price { get; set; }
[Display(Name = "Açıklama")]
public String Description { get; set; }
public Boolean IsChecked { get; set; } //add a property to store whether the item is check or not.
}
View page:
#model List<netcore3_1sample.Models.Item>
#{
ViewData["Title"] = "ItemIndex";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2 class="text-center text-success">#ViewBag.name İsimli Araca Yükleme Yap</h2>
<form asp-controller="Home"
asp-action="LoadItem" method="post">
<br />
<input type="submit" name="submit" value="Oluştur" class="btn btn-primary" />
<br />
<table class="table table-bordered table-striped table-hover">
<thead>
<tr>
<th class="text-center">
Yüklensin mi?
</th>
<th class="text-center">
Name
</th>
<th class="text-center">
Price
</th>
<th class="text-center">
Description
</th>
</tr>
</thead>
<tbody>
#for( var i = 0; i < Model.Count;i++)
{
<tr>
<td>
<div class="form-check">
<label class="form-check-label" for="#Model[i].Id" asp-validation-for="#Model[i].Id"></label>
<input class="form-check-input" name="ids" type="checkbox" value="#Model[i].Id" id="#Model[i].Id">
#*<input type="checkbox" asp-for="#Model[i].IsChecked" />*#
<input type="hidden" asp-for="#Model[i].Id" />
</div>
</td>
<td>
#Html.DisplayFor(modelItem => Model[i].Name)
<input type="hidden" asp-for="#Model[i].Name" />
</td>
<td>
₺#Html.DisplayFor(modelItem => Model[i].Price)
<input type="hidden" asp-for="#Model[i].Price" />
</td>
<td>
#Html.DisplayFor(modelItem => Model[i].Description)
<input type="hidden" asp-for="#Model[i].Description" />
</td>
</tr>
}
</tbody>
</table>
</form>
Code in the controller:
[HttpPost]
public ActionResult LoadItem(List<Item> model, int[] ids)
{
return RedirectToAction("ItemIndex");
}
According to your code, you are using a html checkbox element to store the selected Item ID, so here we need to add an array to get the selected ids.
Besides, you could add a IsChecked property in the Item model, then change the following code:
<input class="form-check-input" name="ids" type="checkbox" value="#Model[i].Id" id="#Model[i].Id">
to
<input type="checkbox" asp-for="#Model[i].IsChecked" />
By using this method, in the controller, you could filter the selected item based on the IsChecked property.
The result like this:

How to HTML Content bind in Iframe using MVC razor view?

Create TestModel class in the project show below code.
public class TestModel
{
public long EmailTemplateId { get; set; }
public string TemplateName { get; set; }
public string TemplateContent { get; set; }
public Nullable<System.DateTime> CreatedDate { get; set; }
public Nullable<System.DateTime> UpdatedDate { get; set; }
public Nullable<bool> IsDefaultTemplate { get; set; }
public string TemplateKey { get; set; }
public string TemplatePath { get; set; }
public string TemplateJson { get; set; }
public Nullable<bool> IsAdmin { get; set; }
public int TemplateType { get; set; }
public string TemplateTag { get; set; }
}
Create GetTestlst action method to the controller and get all data and list pass test.cshtml
public ActionResult GetTestlst()
{
var templist = GetTestList().ToList();
return View(templist);
}
below code written Test.cshtml page and get the model list in controller to view.
#using test.Helpers;
#model List<test.Entities.TestModel>
#{
ViewBag.Title = "Template";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Index</h2>
<table class="table">
<tr>
<th>
#Html.DisplayNameFor(model => model[0].EmailTemplateId)
</th>
<th>
#Html.DisplayNameFor(model => model[0].TemplateName)
</th>
<th>
#Html.DisplayNameFor(model => model[0].TemplateContent)
</th>
<th>
#Html.DisplayNameFor(model => model[0].CreatedDate)
</th>
<th>
#Html.DisplayNameFor(model => model[0].UpdatedDate)
</th>
<th>
#Html.DisplayNameFor(model => model[0].IsDefaultTemplate)
</th>
<th>
#Html.DisplayNameFor(model => model[0].TemplateKey)
</th>
<th>
#Html.DisplayNameFor(model => model[0].TemplatePath)
</th>
<th>
#Html.DisplayNameFor(model => model[0].TemplateJson)
</th>
<th>
#Html.DisplayNameFor(model => model[0].IsAdmin)
</th>
<th>
#Html.DisplayNameFor(model => model[0].TemplateType)
</th>
<th>
#Html.DisplayNameFor(model => model[0].TemplateTag)
</th>
<th></th>
</tr>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.EmailTemplateId)
</td>
<td>
#Html.DisplayFor(modelItem => item.TemplateName)
</td>
<td>
**`<iframe id="testiframe" class="iframe" src=#item.TemplateContent></iframe>`**
</td>
<td>
#Html.DisplayFor(modelItem => item.CreatedDate)
</td>
<th>
#Html.DisplayFor(model => item.UpdatedDate)
</th>
<th>
#Html.DisplayFor(model => item.IsDefaultTemplate)
</th>
<th>
#Html.DisplayFor(model => item.TemplateKey)
</th>
<th>
#Html.DisplayFor(model => item.TemplatePath)
</th>
<th>
#Html.Raw(item.TemplateJson)
</th>
<th>
#Html.DisplayFor(model => item.IsAdmin)
</th>
<th>
#Html.DisplayFor(model => item.TemplateType)
</th>
<th>
#Html.DisplayFor(model => item.TemplateTag)
</th>
<td>
#*#Html.ActionLink("Edit", "Edit", "Test", new { id = item.Id }, null) |
#Html.ActionLink("Details", "Details", "Test", new { id = item.Id }, null) |
#Html.ActionLink("Delete", "Delete", "Test", new { id = item.Id }, null)*#
</td>
</tr>
}
</table>
Show output below image.
Iframe proper but not show Html content in the model list. please give the answer.
Have you considered loading the HTML into a container?
<div id='#item.EmailTemplateId'></div>
<script type="text/javascript">
$(document).ready(function(){
$('##item.EmailTemplateId').load('#item.');
});
</script>
Old Code replace to below code
<iframe id="testiframe" class="iframe" src=#item.TemplateContent></iframe>
I solved this issue
<div id=#item.EmailTemplateId style="height:200px; overflow:scroll;">#Html.Raw(HttpUtility.HtmlDecode(item.TemplateContent))</div>

selected value in dropdownlist is not working

I am trying to display a complex viewmodel in a view, everything works fine except one dropdownlist. I am displaying a list of enum, and set the selected object as it should be. But for an unknown reason, its not showing the selected value. I have tried many times with but failed -- please help me. The code is given below -
Enum:
public enum CommentStatus
{
NoStatus = 0,
Resolved = 1,
NotApplicable = 2
}
Model:
public class CheckComments
{
public string EntryId { get; set; }
public int Serial { get; set; }
public string Comment { get; set; }
public DateTime CommentDate { get; set; }
public CommentStatus CommentStatus { get; set; }
}
public class CheckDataViewModel
{
public string EntryId { get; set; }
public string CheckId { get; set; }
public decimal Value { get; set; }
public List<CheckComments> CheckCommentsList { get; set; }
public List<string> CommentStatusList { get; set; }
}
Controller:
public class CheckDataController : Controller
{
public ActionResult GetEnteredCheckData(string entryId)
{
var checkDataViewModel = new CheckDataViewModel();
var checkData = new CheckDataManager().GetCheckData(entryId);
checkDataViewModel.EntryId = checkData.EntryId;
checkDataViewModel.CheckId = checkData.CheckId;
checkDataViewModel.Value = checkData.Value;
checkDataViewModel.CheckCommentsList = checkData.Comments;
checkDataViewModel.CommentStatusList = Enum.GetNames(typeof (CommentStatus)).ToList();
return View("EnteredCheckData", checkDataViewModel);
}
}
View:
#model PDCA.Web.Models.CheckDataViewModel
#{
ViewBag.Title = "EnteredCheckData";
Layout = "~/Views/Shared/_Layout.cshtml";
}
#using (Html.BeginForm())
{
#Html.ValidationSummary(true)
<fieldset>
<div class="row">
<div class="span3">
<div class="text-left">#Html.LabelFor(model => model.EntryId)</div>
</div>
<div class="span3">
<div class="text-left">
#Html.DisplayFor(model => model.EntryId)
#Html.HiddenFor(model => model.EntryId)
</div>
</div>
</div>
</fieldset>
<fieldset>
<legend>Check Information</legend>
<div class="row">
<div class="span3">
<div class="text-left">#Html.LabelFor(model => model.CheckId)</div>
</div>
<div class="span3">
<div class="text-left">
#Html.DisplayFor(model => model.Check.CheckId)
#Html.HiddenFor(model => model.Check.CheckId)
</div>
</div>
</div>
</fieldset>
<fieldset>
<legend>Comments</legend>
<div>
<table class="table table-bordered">
<tr>
<th>
Serial
</th>
<th>
Comment
</th>
<th>
Date
</th>
<th>
Status
</th>
<th>
</th>
</tr>
#for (int i = 0; i < Model.CheckCommentsList.Count; i++)
{
<tr>
<td>
#Html.DisplayFor(modelItem => Model.CheckCommentsList[i].Serial)
#Html.HiddenFor(modelItem => Model.CheckCommentsList[i].Serial)
</td>
<td>
#Html.DisplayFor(modelItem => Model.CheckCommentsList[i].Comment)
#Html.HiddenFor(modelItem => Model.CheckCommentsList[i].Comment)
</td>
<td>
#Html.DisplayFor(modelItem => Model.CheckCommentsList[i].CommentDate)
#Html.HiddenFor(modelItem => Model.CheckCommentsList[i].CommentDate)
</td>
<td>
#Html.DropDownListFor(modelItem => Model.CheckCommentsList[i].CommentStatus, new SelectList(Model.CommentStatusList, Model.CheckCommentsList[i].CommentStatus))
#Html.ValidationMessageFor(model => Model.CheckCommentsList[i].CommentStatus)
</td>
<td>
#Html.ActionLink("Update Status", "UpdateCommentStatus", new { Model.EntryId, Model.CheckCommentsList[i].Serial, Model.Check.CheckTitle, Model.Check.CheckId })|
#Html.ActionLink("Delete", "DeleteComment", new { Model.CheckCommentsList[i].Serial, Model.EntryId })
</td>
</tr>
}
</table>
</div>
</fieldset>
}
<div>
#Html.ActionLink("Back to List", "Index")
</div>
The code is too lengthy but I think this will help you to understand my mistakes. Thanks in advance friends.
Solution:
#Html.DropDownListFor(modelItem => Model.CheckCommentsList[i].CommentStatus, new SelectList(Model.CommentStatusList, Model.CheckCommentsList[i].CommentStatus.ToString()))

Updating multiple items within same view

I am trying to make a stock take application, My view loads all my stock with one editor.
My controller is not getting any of the data from the view?
I want to be able to edit all my stock at the same time?
How can I do this
Model Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace FlatSystem.Models
{
public class Stock
{
public int ID { get; set; }
public string Item_Name { get; set; }
public int Actual_Stock { get; set; }
public int Wanted_Stock { get; set; }
}
}
View Code
#model IEnumerable<FlatSystem.Models.Stock>
#using (Html.BeginForm())
{
#Html.ValidationSummary(true)
<div class="sidemenu">
<div class="sidemenu-heading">
ReStock
</div>
<div class="div-body">
<table>
<tr>
<th>
Item Name
</th>
<th>
Wanted Stock
</th>
<th>
Stock On Hand
</th>
<th></th>
</tr>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.Item_Name)
</td>
<td>
#Html.DisplayFor(modelItem => item.Wanted_Stock)
</td>
<td>
<div class="editor-field">
#Html.EditorFor(modelItem => item.Actual_Stock)
#Html.ValidationMessageFor(modelItem => item.Actual_Stock)
</div>
</td>
#Html.HiddenFor(modelItem => item.ID)
</tr>
}
</table>
</div>
</div>
<input type="submit" value="Submit" />
}
Controller Code
[HttpPost]
public ActionResult ReStock(List<Stock> stock)
{
foreach (var item in stock)
{
if (ModelState.IsValid)
{
GR.InsertOrUpdate(item);
}
}
GR.Save();
return RedirectToAction("Restock");
}
It's hard to answer your question without model class, but idea is that your edit inputs must contain index in name attribute.
Something like this:
#for(int i = 0: i < Model.Count(); i++)
{
<tr>
<td>
#Html.DisplayFor(modelItem => Model[i].Item_Name)
</td>
<td>
#Html.DisplayFor(modelItem => Model[i].Wanted_Stock)
</td>
<td>
<div class="editor-field">
#Html.EditorFor(modelItem => Model[i].Actual_Stock)
#Html.ValidationMessageFor(modelItem => Model[i].Actual_Stock)
</div>
</td>
#Html.HiddenFor(modelItem => Model[i].ID)
</tr>
}
Added:
Sorry, thanks to Darin Dimitrov, you can't access IEnumerable by index, use List or Array.
You could use editor templates. I would recommend you to first read the following article in order to understand why your code doesn't correctly bind the collection. Once you understand that you could do the following:
#model IEnumerable<FlatSystem.Models.Stock>
#using (Html.BeginForm())
{
#Html.ValidationSummary(true)
<div class="sidemenu">
<div class="sidemenu-heading">
ReStock
</div>
<div class="div-body">
<table>
<thead>
<tr>
<th>Item Name</th>
<th>Wanted Stock</th>
<th>Stock On Hand</th>
<th></th>
</tr>
</thead>
<tbody>
#Html.EditorForModel()
</tbody>
</div>
</div>
<input type="submit" value="Submit" />
}
and now define a custom editor template for the Stock type which will automatically be rendered for each element of the collection (~/Views/Shared/EditorTemplates/Stock.cshtml) - the name and location of the editor template is important as it works by convention:
#model FlatSystem.Models.Stock
<tr>
<td>
#Html.DisplayFor(x => x.Item_Name)
</td>
<td>
#Html.DisplayFor(x => x.Wanted_Stock)
</td>
<td>
<div class="editor-field">
#Html.EditorFor(x => x.Actual_Stock)
#Html.ValidationMessageFor(x => x.Actual_Stock)
</div>
</td>
#Html.HiddenFor(x => x.ID)
</tr>
Remark: You might also want to include the Wanted_Stock and Item_Name as hidden fields along with the ID in the editor template in order for their values to be sent to the server, because you don't have a corresponding input field for them.

MVC Model on Post has no navigation properties

I've looked around SO and I can't find anyone with an issue like this, so I'm posting this question.
I have a two edit actions in a controller (post and get) and in the GET I populate the Wine property of a viewmodel that gets passed to the view.
On the POST, I read that viewmodel, and save the changes to the database. After that I wanted to update my Lucene seach index with the updated Wine property. However, I keep getting an error because the Wine property doesn't have any of its navigation properties populated.
I tried looking up a new wine object using db.Wines.Find(ew.wine.WindID) but that still returns a Wine with no navigation properties. It is worth mentioning this is exactly what the GET edit action does, and the is populated correctly. I have no idea what is going on here. I also tried Wine w = db.Entry(ew.Wine).Entity to get those navigation properties populated, but to no avail... Thanks for the help in advance!
ViewModel & Wine Model
public class NewWineViewModel
{
public Wine Wine { get; set; }
public VOAVIRequest VOAVIRequest { get; set; }
public bool IsRequest { get; set; }
public SelectList VarTypes { get; set; }
public SelectList Origins { get; set; }
public SelectList Apps { get; set; }
public SelectList Vintages { get; set; }
public SelectList Importers { get; set; }
public NewWineViewModel()
{
this.Wine = new Wine();
}
}
public class Wine :Updater
{
public int WineID { get; set; }
//public int WineTypeID { get; set; }
[Display(Name = "Varietal/Type")]
public int? VarTypeID { get; set; }
[Display(Name = "Origin")]
public int? OriginID { get; set; }
[Display(Name = "Appellation")]
public int? AppID { get; set; }
[Display(Name = "Vintage")]
public int? VintageID { get; set; }
[Display(Name = "Importer")]
public int? ImporterID { get; set; }
public int ProducerID { get; set; }
public string Designate { get; set; }
[Display(Name = "Drink Window")]
public string DrinkWindow { get; set; }
public string Body { get; set; }
public string SKU { get; set; }
[Display(Name = "Varietal Makeup")]
public string VarietalMakeup { get; set; }
[Display(Name = "Case Production")]
public string CaseProduction { get; set; }
[Display(Name = "Alcohol Content")]
public double? AlcoholContent { get; set; }
public string Winemaker { get; set; }
[Display(Name = "Consulting Winemaker")]
public string ConsultWinemaker { get; set; }
public bool Sustainable { get; set; }
public bool Kosher { get; set; }
public bool Organic { get; set; }
public bool Biodynamic { get; set; }
public bool SalmonSafe { get; set; }
public Boolean Active { get; set; }
[Display(Name = "ResidualSugar")]
public double? RS { get; set; }
public double? pH { get; set; }
public string QRUrl { get; set; }
public virtual WineType WineType { get; set; }
public virtual VarType VarType { get; set; }
public virtual Origin Origin { get; set; }
public virtual App App { get; set; }
public virtual Vintage Vintage { get; set; }
public virtual Importer Importer { get; set; }
public virtual Producer Producer { get; set; }
}
Two Edit Actions from the controller:
[ProducerEdit]
public ActionResult Edit(int WineID)
{
NewWineViewModel ew = new NewWineViewModel();
ew.Wine = db.Wines.Find(WineID);
ew.VarTypes = new SelectList(db.VarTypes, "VarTypeID", "Name", ew.Wine.VarTypeID);
ew.Origins = new SelectList(db.Origins, "OriginID", "Name", ew.Wine.OriginID);
ew.Apps = new SelectList(db.Apps, "AppID", "Name", ew.Wine.AppID);
ew.Vintages = new SelectList(db.Vintages, "VintageID", "Name", ew.Wine.VintageID);
//might need to handle null on this one
ew.Importers = new SelectList(db.Importers, "ImporterID", "Name", ew.Wine.ImporterID);
return View(ew);
}
//post
[HttpPost]
[ProducerEdit]
public ActionResult Edit(NewWineViewModel ew)
{
if (ModelState.IsValid)
{
ew.Wine.Body = ew.Wine.Body == "Please select wine body" ? string.Empty : ew.Wine.Body;
ew.Wine.UpdatedBy = User.Identity.Name;
ew.Wine.UpdatedOn = DateTime.Now;
db.Entry(ew.Wine).State = EntityState.Modified;
db.SaveChanges();
Wine w = db.Wines.Find(ew.Wine.WineID);
Lucene.LuceneSearch.ClearLuceneIndexRecord(ew.Wine.WineID);
Lucene.LuceneSearch.AddUpdateLuceneIndex(ew.Wine);
if (ew.IsRequest)
{
ew.VOAVIRequest.WineID = ew.Wine.WineID;
ew.VOAVIRequest.CreatedBy = User.Identity.Name;
ew.VOAVIRequest.CreatedOn = DateTime.Now;
db.VOAVIRequests.Add(ew.VOAVIRequest);
db.SaveChanges();
return RedirectToAction("Requested");
//redirect to "Request Submitted" page for new wines
}
return RedirectToAction("Details", new { id = ew.Wine.WineID });
}
else
{
ew.VarTypes = new SelectList(db.VarTypes, "VarTypeID", "Name", ew.Wine.VarTypeID);
ew.Origins = new SelectList(db.Origins, "OriginID", "Name", ew.Wine.OriginID);
ew.Apps = new SelectList(db.Apps, "AppID", "Name", ew.Wine.AppID);
ew.Vintages = new SelectList(db.Vintages, "VintageID", "Name", ew.Wine.VintageID);
//might need to handle null on this one
ew.Importers = new SelectList(db.Importers, "ImporterID", "Name", ew.Wine.ImporterID);
return View(ew);
}
}
EDIT - here is the form in Razor
#using (Html.BeginForm())
{
#Html.ValidationSummary(true)
<h3>#ViewBag.ProducerName</h3>
#Html.HiddenFor(m => m.Wine.WineID)
#Html.HiddenFor(m => m.Wine.ProducerID)
#Html.HiddenFor(m => m.IsRequest)
<h4 style="margin-top: -40px; padding-bottom: 10px;">Editing #Model.Wine.Name</h4>
<table>
<tr>
<td>#Html.LabelFor(m => m.Wine.VarTypeID)
</td>
<td style="display: inline">
<div class="voavi-select">
#Html.DropDownListFor(m => m.Wine.VarTypeID, Model.VarTypes, new { #class = "chzn-select" })
</div>
#Html.TextBoxFor(m => m.VOAVIRequest.VarType, new { style = "display: none;", #class = "voavignore" })
<a id="lnkNewVar" class="filetypes" href="#">New Varietal?</a> #* #Html.ValidationMessageFor(m => m.VOAVIRequest.VarType)*#
</td>
</tr>
<tr>
<td>
#Html.LabelFor(m => m.Wine.OriginID)
</td>
<td>
<div class="voavi-select">
#Html.DropDownListFor(m => m.Wine.OriginID, Model.Origins, new { #class = "chzn-select" })
</div>
#Html.TextBoxFor(m => m.VOAVIRequest.Origin, new { style = "display: none;", #class = "voavignore" })
<a id="lnkNewOrigin" class="filetypes" href="#">New Origin?</a>
</td>
</tr>
<tr>
<td>
#Html.LabelFor(m => m.Wine.AppID)
</td>
<td>
<div class="voavi-select">
#Html.DropDownListFor(m => m.Wine.AppID, Model.Apps, new { #class = "chzn-select" })
</div>
#Html.TextBoxFor(m => m.VOAVIRequest.App, new { style = "display: none;", #class = "voavignore" })<a
id="lnkNewApp" class="filetypes" href="#">New Varietal?</a>
</td>
</tr>
<tr>
<td>
#Html.LabelFor(m => m.Wine.VintageID)
</td>
<td>
<div class="voavi-select">
#Html.DropDownListFor(m => m.Wine.VintageID, Model.Vintages, new { #class = "chzn-select" })
</div>
#Html.TextBoxFor(m => m.VOAVIRequest.Vintage, new { style = "display: none;", #class = "voavignore" })
<a id="lnkNewVintage" class="filetypes" href="#">New Varietal?</a>
</td>
</tr>
<tr>
<td>
#Html.LabelFor(m => m.Wine.Designate)
</td>
<td>
#Html.EditorFor(m => m.Wine.Designate)
</td>
</tr>
<tr>
<td>
#Html.LabelFor(m => m.Wine.DrinkWindow)
</td>
<td>
#Html.EditorFor(m => m.Wine.DrinkWindow)
</td>
</tr>
<tr>
<td>
#Html.LabelFor(m => m.Wine.VarietalMakeup)
</td>
<td>
#Html.EditorFor(m => m.Wine.VarietalMakeup)
</td>
</tr>
<tr>
<td>
#Html.LabelFor(m => m.Wine.Body)
</td>
<td>
#Html.DropDownListFor(m => m.Wine.Body, new SelectList(Model.Wine.BodyList, "Text", "Text", Model.Wine.Body), new { #class = "chzn-select" })
</td>
</tr>
<tr>
<td>
#Html.LabelFor(m => m.Wine.ImporterID)
</td>
<td>
<div class="voavi-select">
#Html.DropDownListFor(m => m.Wine.ImporterID, Model.Importers, new { #class = "chzn-select" })</div>
#Html.TextBoxFor(m => m.VOAVIRequest.Importer, new { style = "display: none;" })
<a id="lnkNewImporter" class="filetypes" href="#">New Varietal?</a>
</td>
</tr>
<tr>
<td>
#Html.LabelFor(m => m.Wine.SKU)
</td>
<td>
#Html.EditorFor(m => m.Wine.SKU)
</td>
</tr>
<tr>
<td>
#Html.LabelFor(m => m.Wine.CaseProduction)
</td>
<td>
#Html.EditorFor(m => m.Wine.CaseProduction)
</td>
</tr>
<tr>
<td>
#Html.LabelFor(m => m.Wine.AlcoholContent)
</td>
<td>
#Html.EditorFor(m => m.Wine.AlcoholContent)
</td>
</tr>
<tr>
<td>
#Html.LabelFor(m => m.Wine.RS)
</td>
<td>
#Html.EditorFor(m => m.Wine.RS)
</td>
</tr>
<tr>
<td>
#Html.LabelFor(m => m.Wine.pH)
</td>
<td>
#Html.EditorFor(m => m.Wine.pH)
</td>
</tr>
<tr>
<td>
#Html.LabelFor(m => m.Wine.Winemaker)
</td>
<td>
#Html.EditorFor(m => m.Wine.Winemaker)
</td>
</tr>
<tr>
<td>
#Html.LabelFor(m => m.Wine.ConsultWinemaker)
</td>
<td>
#Html.EditorFor(m => m.Wine.ConsultWinemaker)
</td>
</tr>
<tr>
<td>
#Html.LabelFor(m => m.Wine.Sustainable)
</td>
<td>
#Html.EditorFor(m => m.Wine.Sustainable)
</td>
</tr>
<tr>
<td>
#Html.LabelFor(m => m.Wine.Kosher)
</td>
<td>
#Html.EditorFor(m => m.Wine.Kosher)
</td>
</tr>
<tr>
<td>
#Html.LabelFor(m => m.Wine.Organic)
</td>
<td>
#Html.EditorFor(m => m.Wine.Organic)
</td>
</tr>
<tr>
<td>
#Html.LabelFor(m => m.Wine.Biodynamic)
</td>
<td>
#Html.EditorFor(m => m.Wine.Biodynamic)
</td>
</tr>
<tr>
<td>
#Html.LabelFor(m => m.Wine.SalmonSafe)
</td>
<td>
#Html.EditorFor(m => m.Wine.SalmonSafe)
</td>
</tr>
</table>
<p>
<input type="submit" value="Update" />
</p>
}

Resources