How to submit only one item from a list - asp.net-mvc

I have a list of objects and i want to be able to do is when a user clicks the button associated with an item from the list, that item is displayed in another view
#model VitalBits_2._0.Models.VitalBitListModel
<html>
<head>
<script src="~/Scripts/jquery-3.3.1.min.js"></script>
<script>
</script>
</head>
<body>
<table>
<tr>
<th style="align-content:center">
Title
</th>
<th style="align-content:center">
Vital Bit Type
</th>
<th>
</th>
</tr>
#for (int i = 0; i < Model.VitalBitList.Count; i++)
{
using (Html.BeginForm("PopulateEdit_Create", "Configuration", FormMethod.Post))
{
<tr>
<td style="align-content:center">
#Html.HiddenFor(x => Model.VitalBitList[i].ID)
#Html.DisplayFor(x => Model.VitalBitList[i].Title)
#Html.HiddenFor(x => Model.VitalBitList[i].Title)
</td>
<td style="align-content:center">
#Html.DisplayFor(x => Model.VitalBitList[i].Message)
#Html.HiddenFor(x => Model.VitalBitList[i].Message)
#Html.HiddenFor(x => Model.VitalBitList[i].IsActive)
#Html.HiddenFor(x => Model.VitalBitList[i].IsPriority)
#Html.HiddenFor(x => Model.VitalBitList[i].BitType)
#Html.HiddenFor(x => Model.VitalBitList[i].Created)
#Html.HiddenFor(x => Model.VitalBitList[i].CreatedBy)
#Html.HiddenFor(x => Model.VitalBitList[i].LastModified)
#Html.HiddenFor(x => Model.VitalBitList[i].LastModifiedBy)
</td>
<td>
<button type="submit">Edit</button>
</td>
</tr>
}
}
</table>
<div id="CreateEdit"></div>
</body>
This is how my code currently stands but i am currently getting my constructor values instead of the item selected
[HttpPost]
public ActionResult PopulateEdit_Create(VitalBit selectedItem) {
DataFactory dataFactory = new DataFactory();
//Logic and stuff
return View("~/Views/Configuration/Create_Edit.cshtml", selectedItem);
}
In other words can i only submit one item to my controller at a time, or do i have to do it in a different way?
Any help would be appreciated

Related

How to render a table from a view in the footer section of _Layout view?

I am developing a solution to enter project-split times in the fullcalendar jquery application. A SQL view shows incomplete days (where entered time is not equal to 8 hours) and renders the table from the entity model in the Generate view:
#model IEnumerable<support_tickets.Models.DailyHours>
#{
Layout = "~/Views/Shared/_Layout.cshtml";
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Generate</title>
</head>
<body>
<table class="table">
<tr>
<th>
#Html.DisplayNameFor(model => model.Start)
</th>
<th>
#Html.DisplayNameFor(model => model.Week)
</th>
<th>
#Html.DisplayNameFor(model => model.Hours)
</th>
<th></th>
</tr>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.Start)
</td>
<td>
#Html.DisplayFor(modelItem => item.Week)
</td>
<td>
#Html.DisplayFor(modelItem => item.Hours)
</td>
</tr>
}
</table>
</body>
</html>
I'd like to render the same table in the footer section of the _Layout view, to see everything on one page. Any assistance is greatly appreciated. Thanks
This is what worked for me - creating a partial view from the PartialViewResult action.
In Controller:
public PartialViewResult IncompleteDays()
{
ticketsEntities entities = new ticketsEntities();
var ent = entities.DailyHours.ToList();
return PartialView(ent);
}
Create a partial View from the action, with the table:
#model IEnumerable<support_tickets.Models.DailyHours>
<div class="row justify-content-center">
<div class="col-auto">
<table class="table table-striped">
<tr>
<th>
#Html.DisplayNameFor(model => model.Start)
</th>
<th>
#Html.DisplayNameFor(model => model.Week)
</th>
<th>
#Html.DisplayNameFor(model => model.Hours)
</th>
<th></th>
</tr>
#foreach (var item in Model)
{
<tr>
<td>
#Convert.ToDateTime(item.Start).ToString("MM-dd-yyyy")
</td>
<td>
#Html.DisplayFor(modelItem => item.Week)
</td>
<td>
#Html.DisplayFor(modelItem => item.Hours)
</td>
</tr>
}
</table>
</div>
</div>
And then call the action from the footer section of the _Layout view:
#{ Html.RenderAction("IncompleteDays", "Home");}

Partial View linkage to controller and model [duplicate]

This question already has answers here:
#Html.Action in Asp.Net Core
(10 answers)
Closed 6 years ago.
I am new to .Net and MVC and I seem to hit a problem that I can´t fix. I am trying to follow a couple of tutorials but I have failed when it comes to partial views. I looked up a lot of answers here and on different websites and still nothing works. I can´t figure out what I am doing wrong.
This is my partial controller:
public class PartialController : Controller
{
public ActionResult _PartialView()
{
var model = new Partial();
model.Count = 2;
return PartialView(model);
}
}
And this is my Partial View:
#model WebApplication1Tutorial.Models.Partial
<div>
The magic number is:
#Html.DisplayFor(model => model.Count)
</div>
I call this view in Index.cshtml by the following command:
<div>
#Html.Partial("/Views/Partial/_PartView.cshtml",new Partial())
</div>
And finally this is the model:
public class Partial
{
public int Count { get; set; }
}
Everything that I tried comes up with the webpage displaying the text and the number 0. I really have no idea what else to do. I know it is something easy but I can´t see it.
Thanks for the help.
Edit. The code for the index view:
#model MovieGenreViewModel
#{
ViewData["Title"] = "Index";
}
<h2>Index</h2>
<p>
<a asp-action="Create">Create New</a>
</p>
<form asp-controller="Movies" asp-action="Index" method="get">
<p>
<select asp-for="movieGenre" asp-items="Model.genres">
<option value="">All</option>
</select>
Title: <input type="text" name="SearchString" >
<input type="submit" value="Filter" />
</p>
</form>
<table class="table">
<thead>
<tr>
<th>
#Html.DisplayNameFor(model => model.movies[0].Genre)
</th>
<th>
#Html.DisplayNameFor(model => model.movies[0].Price)
</th>
<th>
#Html.DisplayNameFor(model => model.movies[0].ReleaseDate)
</th>
<th>
#Html.DisplayNameFor(model => model.movies[0].Title)
</th>
<th>
#Html.DisplayNameFor(model => model.movies[0].Rating)
</th>
<th></th>
</tr>
</thead>
<tbody>
#foreach (var item in Model.movies) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.Genre)
</td>
<td>
#Html.DisplayFor(modelItem => item.Price)
</td>
<td>
#Html.DisplayFor(modelItem => item.ReleaseDate)
</td>
<td>
#Html.DisplayFor(modelItem => item.Title)
</td>
<td>
#Html.DisplayFor(modelItem => item.Rating)
</td>
<td>
<a asp-action="Edit" asp-route-id="#item.ID">Edit</a> |
<a asp-action="Details" asp-route-id="#item.ID">Details</a> |
<a asp-action="Delete" asp-route-id="#item.ID">Delete</a>
</td>
</tr>
}
</tbody>
</table>
<div>
#{ Html.RenderAction("_PartialView"); }
</div>
When you pass your partial like with the #Html.Partial, you also pass a new Model and the _PartialView() method is not invoked so the model is not populated with the value 2.
You could use #Html.Action helper:
<div>
#Html.Action("/Views/Partial/_PartView.cshtml")
</div>

Why do textboxes not clear out after clearing out Model

I have a form which is submitted via ajax to the Controller. The Controller collects the data from the Model and then creates a new ViewModel and passes it to the partial view which is then updated on the Client.
The problem is that the textboxes are not cleared out as expected. The message arrives in the View as expected, so it doesn't make sense that the TextBoxes are not cleared out.
Yet it seems to be a browser issue as this is the resulting HTML, which note, does not have anything in the value:
<input id="Address_Address1" name="Address.Address1" type="text" value="" />
Yet the user sees the value that was priorly entered.
Here is the controller:
//Process Order Here
//This should clear out the ViewModel.
order = new OrderViewModel();
order.Message = "Report Added to your cart";
return PartialView("_NewOrderPartial", order);
This is the partial View:
#model NTC.PropertySearch.Models.OrderViewModel
#using (Ajax.BeginForm("NewOrder", "Order", new AjaxOptions { InsertionMode = InsertionMode.Replace, UpdateTargetId = "neworder" }))
{
<div id="neworder">
<table>
<tr>
<th style="width: 300px;">
<h3>Enter property address below</h3>
</th>
<th style="width: 30px;"></th>
<th style="width: 300px;">
<h3>Choose your report below</h3>
</th>
</tr>
<tr>
<td>
<div class="form">
<table>
<tr>
<td>
#Html.LabelFor(m => m.Address.Address1)
</td>
<td>
#Html.TextBoxFor(m => m.Address.Address1)
</td>
<td>
#Html.ValidationMessageFor(m => m.Address.Address1)
</td>
</tr>
<tr>
<td>
#Html.LabelFor(m => m.Address.Address2)
</td>
<td>
#Html.TextBoxFor(m => m.Address.Address2)
</td>
<td>
#Html.ValidationMessageFor(m => m.Address.Address2)
</td>
</tr>
<tr>
<td>
#Html.LabelFor(m => m.Address.City)
</td>
<td>
#Html.TextBoxFor(m => m.Address.City)
</td>
<td>
#Html.ValidationMessageFor(m => m.Address.City)
</td>
</tr>
<tr>
<td>
#Html.LabelFor(m => m.Address.State)
</td>
<td>
#Html.TextBoxFor(m => m.Address.State)
</td>
<td>
#Html.ValidationMessageFor(m => m.Address.State)
</td>
</tr>
<tr>
<td>
#Html.LabelFor(m => m.Address.ZipCode)
</td>
<td>
#Html.TextBoxFor(m => m.Address.ZipCode)
</td>
<td>
#Html.ValidationMessageFor(m => m.Address.ZipCode)
</td>
</tr>
</table>
<input type="submit" value="Add Report" />
#Html.DisplayFor(m=> m.Message)
</div>
</td>
</tr>
</table>
</div>
The reason for this is because the Html helpers such as TextBox are first looking at the ModelState when binding their values and only after that in the View model. This often happens when people attempt to modify/clear some value to the view model in an HttpPost controller action. The corresponding view will use the initial POSTed value and not the value in the view model.
You should clear the ModelState as well in your controller action:
ModelState.Clear();
order = new OrderViewModel();
Alternatively if you do not want to clear the entire ModelState (although in your case this seems to be the case as you are reinitializing the entire view model) you could clear only some properties that you intend to modify:
ModelState.Remove("SomeProperty");
viewModel.SomeProperty = "some new value";

How to pass a list of objects to a [HTTPPost]controller parameter, in ASP.Net MVC?

i have the next view:
#model IEnumerable<L5ERP.Model.BLL.BusinessObjects.MTR_MonthlyTransfer>
#using (Html.BeginForm("ExpenseMonthlyTransferProcessing", "BudgetTransfer", Model.ToList())){
<table class ="divTable">
<tr>
<th>Transferir</th>
<th>
Clave
</th>
<th>
Monto
</th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#Html.CheckBoxFor(x => item.MTR_Bool, new { #class = "checkMTR", #checked = "checked" })
</td>
<td>
#Html.TextBoxFor(x => item.MTR_Key, new {#class = "longInput" })
</td>
<td>
#String.Format("{0:F}", item.MTR_Amount)
</td>
</tr>
}
</table>
}
and my controller like this
[HttpPost]
public ActionResult ExpenseMonthlyTransferProcessing(List<MTR_MonthlyTransfer> lstMtr)
{ return View(lstMTR); }
But when i do the post my list is null, how can i send my list through the submit button ?
You should change the #model to an array (L5ERP.Model.BLL.BusinessObjects.MTR_MonthlyTransfer[]) or something else that implements IList<>:
#model L5ERP.Model.BLL.BusinessObjects.MTR_MonthlyTransfer[]
#for (var i = 0; i < Model.Length; i ++) {
<tr>
<td>
#Html.CheckBoxFor(x => Model[i].MTR_Bool, new { #class = "checkMTR", #checked = "checked" })
</td>
<td>
#Html.TextBoxFor(x => Model[i].MTR_Key, new {#class = "longInput" })
</td>
<td>
#String.Format("{0:F}", item.MTR_Amount)
</td>
</tr>
receive a FormCollection and parse the items in it manually
Use F12 to check the post in your navigator to see if it are sending the content you expected.

Why the value is changed after clicking the submit button?

I make one action method for 2 activities (new input and edit), and there is also only one
view to handle those activities.
But I don't understand no matter what activity happen, the action method always think it is a new input.
I learned that it because of the ID is always 0, but the problem is, when doing the edit, when in the view the ID is correct as the ID of the data, but when I click the submit button, the action method just see the 0 value of ID.
Here is the action method I used:
[HttpPost]
public ActionResult AddAssignment(SateliteSchedule SatSched)
{
var txt = "";
if (ModelState.IsValid)
{
if (SatSched.ID == 0)
{
db.SateliteSchedules.Add(SatSched);
txt = "{0} has been added!";
}
else
{
db.Entry(SatSched).State = EntityState.Modified;
txt = "{0} has been modified!";
}
db.SaveChanges();
Utility utl = new Utility();
TempData["message"] = string.Format(txt, utl.GetSateliteName(SatSched.SateliteID));
return RedirectToAction("FormAssignment");
}
else
{
ViewBag.Message = "ModelState is not Valid!";
return View("ErrorView");
}
}
And here is the view:
#using (Html.BeginForm("AddAssignment", "admin", FormMethod.Post))
{
#Html.ValidationSummary(true)
<table>
<tr>
<td>#Html.LabelFor(m => m.Tanggal)
</td>
<td>
#Html.EditorFor(m => m.Tanggal)
#Html.ValidationMessageFor(m => m.Tanggal)
</td>
</tr>
<tr>
<td>#Html.LabelFor(m => m.SateliteID)</td>
<td>
#Html.DropDownList("SateliteID", (IEnumerable<SelectListItem>)ViewBag.SatList, "--- Satelite ---")
#Html.ValidationMessageFor(m => m.SateliteID)
</td>
</tr>
<tr>
<td>#Html.LabelFor(m => m.WMOnDuty)</td>
<td>
#Html.DropDownList("WMOnDuty", (IEnumerable<SelectListItem>)ViewBag.WMList, "--- Worship Manager ---")
#Html.ValidationMessageFor(m => m.WMOnDuty)
</td>
</tr>
<tr>
<td>#Html.LabelFor(m => m.SMOnDuty)</td>
<td>#Html.EditorFor(m => m.SMOnDuty)</td>
</tr>
<tr>
<td>#Html.LabelFor(m => m.WLOnDuty)</td>
<td>#Html.EditorFor(m => m.WLOnDuty)</td>
</tr>
<tr>
<td>#Html.LabelFor(m => m.MLOnDuty)</td>
<td>#Html.EditorFor(m => m.MLOnDuty)</td>
</tr>
<tr>
<td>#Html.LabelFor(m => m.SoundMan)</td>
<td>#Html.EditorFor(m => m.SoundMan)</td>
</tr>
<tr>
<td valign=top>#Html.LabelFor(m => m.Note)</td>
<td>#Html.TextAreaFor(model => model.Note, new { #class = "memo-text" })</td>
</tr>
</table>
<div>
<input type="submit" value="Save" />
#Html.ActionLink("Kembali", "FormAssignment")
</div>
}
What should I check to fix this?
You have to have the Id as a hidden, so when you go in the method the model will have the id asigned to it (does it make sense?). try placing this in your form
#Html.HiddenFor(m => m.ID)

Resources