I am trying to figure out the best way to present/save the data in a view I am creating.
I am not wedded to the underlying table structure, and have played around with it trying to get the best outcome but no luck.
Effectively I have multiple textbox's that need to be saved back to the database, but due to the view layout (and it really needs to stay in this layout), I need to populate additional columns with hard coded info.
In my view I have just repeated the textbox ( I tried to index it, but got an error saying it wasn't possible on decimal data types?)
Below is my model
namespace Testing.Models { public partial class OnFarm { public int Id { get; set; } public Guid? Tracker { get; set; } public int Year { get; set; } public decimal? Period1 { get; set; } public decimal? Period2 { get; set; } public decimal? Period3 { get; set; } public decimal? Period4 { get; set; } public decimal? Period5 { get; set; } public decimal? Period6 { get; set; } public decimal? Period7 { get; set; } public decimal? Period8 { get; set; } public decimal? Period9 { get; set; } public decimal? Period10 { get; set; } public decimal? Period11 { get; set; } public decimal? Period12 { get; set; } public int Version { get; set; } public int Account { get; set; } public int MvtType { get; set; } public virtual Account AccountNavigation { get; set; } public virtual MovementType MvtTypeNavigation { get; set; } public virtual ProductionModule TrackerNavigation { get; set; } public virtual Version VersionNavigation { get; set; } public virtual Years YearNavigation { get; set; } } }
This is my controller as it stands currently
public IActionResult Create(Guid? id) { var query = _context.LegalEntities.FromSqlRaw("Select DISTINCT B.* From Client.Groups a Left join Client.[Legal Entities] b on b.Parent = a.ID left join client.Enterprises c on c.Parent = b.ID left join client.[Production Module] d on d.Parent = c.ID left join OnFarm.[On Farm] e on e.Tracker = d.ID Where e.id IS NOT NULL AND a.id = {0}", id).ToList(); List<SelectListItem> LEList = new List<SelectListItem>(); foreach (var m in query) { LEList.Add(new SelectListItem { Text = m.Name, Value = m.Id.ToString() }); } ViewBag.LE = LEList; var acclist = _context.Accounts.Where(x => x.AccountCatId == 92100).ToList().OrderBy(x => x.Description); List<SelectListItem> listac = new List<SelectListItem>(); listac.Add(new SelectListItem { Text = "--Select Livestock Type--", Value = "0" }); if (acclist != null) { var i = 1; foreach (var x in acclist) { listac.Add(new SelectListItem { Text = x.Description, Value = x.Number.ToString() }); i += 1; } } ViewBag.Account = listac; ViewData["MvtType"] = new SelectList(_context.MovementTypes, "Id", "Type"); ViewData["Tracker"] = new SelectList(_context.ProductionModules, "Id", "Name"); ViewData["Version"] = new SelectList(_context.Versions, "VersionId", "VersionName"); ViewData["Year"] = new SelectList(_context.Year, "Id", "Year"); return View(); }
Here is my view
<div class="row"> @foreach (var item in ViewBag.LE) { <div class="panel panel-default rounded shadow col-11"> <div class="panel-heading border-bottom"> <div class="no-gutters"><h4><b>@item.Text</b></h4></div> </div> <div class="panel-body"> <div class="row"> @foreach (var i in ViewBag.Tracker) { <div class="row d-flex align-items-center"> <div class="panel panel-default rounded shadow col"> <div class="panel-heading border-bottom"> <div class="no-gutters text-muted col-5"><h3><b> @i.Text</b></h3></div> <div class="col-2"> <select asp-for="Account" class="form-control" asp-items="ViewBag.Account"></select> </div> </div> <div class="panel-body"> <div class="row d-flex align-items-center"> <div class="col-1"><b>Month</b></div> <div class="col-1 d-flex justify-content-center"><b>Opening Balance</b></div> <div class="col-1 d-flex justify-content-center"><b>Births</b></div> <div class="col-1 d-flex justify-content-center"><b>Deaths</b></div> <div class="col-1 d-flex justify-content-center"><b>Purchases</b></div> <div class="col-1 d-flex justify-content-center"><b>Sold</b></div> <div class="col-1 d-flex justify-content-center"><b>Age In</b></div> <div class="col-1 d-flex justify-content-center"><b>Age Out</b></div> <div class="col-1 d-flex justify-content-center"><b>Adjustments</b></div> <div class="col-1 d-flex justify-content-center"><b>Closing Balance</b></div> </div> <div class="row d-flex align-items-center"> <div class="col-1"> Apr </div> <div class="col-1"> @Html.TextBoxFor(m => m.Period1, new { @class = "form-control", disabled = "disabled" }) <span asp-validation-for="Period1" class="text-danger"></span> </div> <div class="col-1"> @Html.TextBoxFor(m => m.Period1, new { @class = "form-control" }) <span asp-validation-for="Period1" class="text-danger"></span> </div> <div class="col-1"> @Html.TextBoxFor(m => m.Period1, new { @class = "form-control" }) <span asp-validation-for="Period1" class="text-danger"></span> </div> <div class="col-1"> @Html.TextBoxFor(m => m.Period1, new { @class = "form-control" }) <span asp-validation-for="Period1" class="text-danger"></span> </div> <div class="col-1"> @Html.TextBoxFor(m => m.Period1, new { @class = "form-control" }) <span asp-validation-for="Period1" class="text-danger"></span> </div> <div class="col-1"> @Html.TextBoxFor(m => m.Period1, new { @class = "form-control" }) <span asp-validation-for="Period1" class="text-danger"></span> </div> <div class="col-1"> @Html.TextBoxFor(m => m.Period1, new { @class = "form-control" }) <span asp-validation-for="Period1" class="text-danger"></span> </div> <div class="col-1"> @Html.TextBoxFor(m => m.Period1, new { @class = "form-control" }) <span asp-validation-for="Period1" class="text-danger"></span> </div> <div class="col-1"> @Html.TextBoxFor(m => m.Period1, new { @class = "form-control", disabled = "disabled" }) <span asp-validation-for="Period1" class="text-danger"></span> </div> </div> </div> </div> </div> } </div> </div> </div> } </div>
Now the panel body is repeated 12 times (for each month of the year).
When saving data back, I will have some hidden fields for some of the core info. BUT what i need to be able to do is when saving back the first text box it also needs to save the MvtType (hard coded as column headings) as a column value.
I will wrap the panels into a form so it will submit when a button is clicked.
Finally, the underlying table. I used to have a column called period, one called value, as thought that might have been a better way of doing it? Rather than each period separated out.
Oh and sorry, I will need to load data to these textboxes when loading the page
Any help is much appreciated
EDIT Ok, so i have made some progress here. I have tided up my view (See below) and can now get my view to pass back the first column of movement types, but nothing in the other columns. I effectively want to be able to post back a value into the period column while adding a value to the Mvt Type Column.
<div class="row"> @foreach (var item in ViewBag.LE) { <div class="panel panel-default rounded shadow col-11"> <div class="panel-heading border-bottom"> <div class="no-gutters"><h4><b>@item.Text</b></h4></div> </div> <div class="panel-body"> <div class="row"> @foreach (var i in ViewBag.Tracker) { <form asp-action="Create" class="form-group"> <div class="form-group"> <label asp-for="Tracker" class="control-label"></label> <select asp-for="Tracker" class="form-control" asp-items="ViewBag.Tracker"></select> </div> <div class="form-group"> <label asp-for="Year" class="control-label"></label> <select asp-for="Year" class="form-control" asp-items="ViewBag.Year"></select> </div> <div class="form-group"> <label asp-for="Version" class="control-label"></label> <select asp-for="Version" class="form-control" asp-items="ViewBag.Version"></select> </div> <div class="row d-flex align-items-center"> <div class="panel panel-default rounded shadow col"> <div class="panel-heading border-bottom"> <div class="no-gutters text-muted col-5"><h3><b> @i.Text</b></h3></div> <div class="col-2"> <select asp-for="Account" class="form-control" asp-items="ViewBag.Account"></select> </div> </div> <div class="panel-body"> <div class="row d-flex align-items-center"> <div class="col-1"><b>Month</b></div> <div class="col-1 d-flex justify-content-center"><b>Opening Balance</b></div> @foreach (var mvt in ViewBag.MVtType) { <div class="col-1 d-flex justify-content-center"> <b>@mvt.Text</b> </div> } <div class="col-1 d-flex justify-content-center"><b>Closing Balance</b></div> </div> <div class="row d-flex align-items-center"> <div class="col-1"> Apr </div> <div class="col-1"> @Html.TextBoxFor(m => m.Period1, new { @class = "form-control", disabled = "disabled" }) <span asp-validation-for="Period1" class="text-danger"></span> </div> @for (int z = 0; z < ViewBag.MvtType.Count; z++) { <div class="col-1"> <select asp-for= "MvtType" class="form-control" asp-items="ViewBag.MvtType"></select> @Html.TextBoxFor(m => m.Period1, new { @class = "form-control" }) <span asp-validation-for="Period1" class="text-danger"></span> </div> } <div class="col-1"> @Html.TextBoxFor(m => m.Period1, new { @class = "form-control", disabled = "disabled" }) <span asp-validation-for="Period1" class="text-danger"></span> </div> </div> </div> </div> </div> <div class="row d-flex align-items-center"> <div class="col-8"></div> <div class="col-2"> <input type="submit" value="Save" class="btn btn-primary float-right" /> </div> </div> </form> } </div> </div> </div> } </div>
https://stackoverflow.com/questions/66466506/mvc-textbox-binding-multiple-properties March 04, 2021 at 07:34AM
没有评论:
发表评论