Asked by:
Displaying database Id of a selected menu option in a text-box.

Question
-
User-345777704 posted
Hie ,i have a drop down-list that shows account types e.g assets ,liabilities etc...these items display from the database..
In the database ,the account types have data associated with them e.g assets has a chart_drawer_ID of 1 ,liabilities a chart_drawer_ID of 2 etc..
what i want to achieve is for me to display the chart_drawer_ID of an account type in a text-box on selecting a particular account type from the drop-down list.
The code below shows the dropdownlist of generating the account type:
<td width="12.5%" scope="row" style="color:Blue;">Account type:</td>
<td>
@(Html.Kendo().DropDownList()
.Name("drawers")
.HtmlAttributes(new { style = "width:214px" })
.OptionLabel("Select Account type...")
.DataTextField("DrawerName")
.DataValueField("DrawerID")
.DataSource(source =>
{
source.Read(read =>
{
read.Action("Get_Chart_Drawers", "../Admin/Chart_Drawers");
});
})
.Events(e =>
{
e.Select("on_Chart_Drawer_Select");
})
)
</td>the code below shows the text-box code for the account code
<td><input type="number" disabled="disabled" class="k-input" id="txtAccountCode" style="
;
right: 266px;
width: 60px;background-color:white;" data-bind="value: Account_Code" /></td>
</tr>the following code shows the text-box being made observable
var accounts_View_Model = {
Account_ID: ko.observable(),
Account_Code: ko.observable(code),}
the following function is for the selection of an account type in the drop down list
function on_Accounting_TypesSelect(e) {
var data_Item = this.data_Item(e.item.index());
accounts_View_Model.Accounting_Type_ID(data_Item.Accounting_Type_ID)
};Monday, July 8, 2019 12:41 PM
All replies
-
User-474980206 posted
normally this would be written in javascript using ajax to retrieve the information and the selects onchange event.
Monday, July 8, 2019 3:06 PM -
User1120430333 posted
You can do a request Request.Form and get the selected value of the Dropdownlist control.
[HttpPost] public ActionResult Create(ProjectViewModels.Project project, string submit) { if (submit == "Cancel") return RedirectToAction("Index"); ValidateddlProjectTypes(); project.ProjectType = (Request.Form["ddlProjectTypes"]); if (ModelState.IsValid && _modelHelper.IsEndDateLessThanStartDate(project, "Project")) ModelState.AddModelError(string.Empty, "End Date cannot be less than Start Date."); if (!ModelState.IsValid) return View(_projectModel.PopulateSelectedList(project)); _projectModel.Create(project, User.Identity.Name); return RedirectToAction("Index"); }
You can take the second approach where the DDL points to the property in a viewmodel where the selected value is kept that can be accessible in the controller using the viewmodel.
using Microsoft.AspNetCore.Mvc.Rendering; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; namespace PublishingCompany.Models { public class PayRollVM { public class Payroll { public int PayrollId { get; set; } public int AuthorId { get; set; } public string AuthorFirstName { get; set; } public string AuthorLastName { get; set; } [Required(ErrorMessage = "Author is required")] public string AuthorTypeId { get; set; } [Required(ErrorMessage = "Salary is required")] public int? Salary { get; set; } public List<SelectListItem> AuthorTypes { get; set; } = new List<SelectListItem>(); } public List<Payroll> Payrolls { get; set; } = new List<Payroll>(); } }
@model PayRollVM.Payroll <!DOCTYPE html> <style type="text/css"> .editor-field > label { float: left; width: 150px; } .txtbox { font-family: Arial, Helvetica, sans-serif; font-size: 12px; background: white; color: black; cursor: text; border-bottom: 1px solid #104A7B; border-right: 1px solid #104A7B; border-left: 1px solid #104A7B; border-top: 1px solid #104A7B; padding-top: 10px; } </style> <html> <head> <title>Create</title> </head> <body> <h1>Author Payroll</h1> @using (Html.BeginForm()) { @Html.ValidationSummary(false, "", new { @class = "text-danger" }) <fieldset> <legend>Create</legend> @Html.HiddenFor(model => model.PayrollId) @Html.HiddenFor(model => model.AuthorId) <div class="form-group"> <div class="editor-field"> @Html.Label("Author:") @Html.DropDownListFor(m => m.AuthorTypeId, Model.AuthorTypes, "Select....") </div> </div> <div class="form-group"> <div class="editor-field"> @Html.Label("Salary:") @Html.TextBoxFor(model => model.Salary) @Html.ValidationMessageFor(model => model.Salary) </div> </div> <br /> <p> <input type="submit" name="submit" value="Save" /> <input type="submit" name="submit" value="Cancel" /> </p> </fieldset> } </body> </html>
[HttpPost] public ActionResult Create(PayRollVM.Payroll payroll, string submit) { if (submit == "Cancel") return RedirectToAction("Index"); if (!ModelState.IsValid) return View(pdm.PopulateSelectedList(payroll)); if (pdm.BlnFindPayRollByAuthorId(int.Parse(payroll.AuthorTypeId))) { ModelState.AddModelError(string.Empty, "Author has an existing PayRoll record."); } if (!ModelState.IsValid) return View(pdm.PopulateSelectedList(payroll)); pdm.Add(payroll); return RedirectToAction("Index"); }
If the above controller that is using the viewmodel, one can address the AuthorTypeId. after ModelState is valid, like getting the ID and placing it in the VM property linked to a textbox and showing the view model in the view.
https://www.tutlane.com/tutorial/aspnet-mvc/how-to-use-viewmodel-in-asp-net-mvc-with-example
Monday, July 8, 2019 3:26 PM