1
Reply

how to retrieve the data of a perticular item from dropdown

athira k

athira k

Feb 10 2017 2:36 AM
224

I am new to work with MVC and I have confusion that how to retrieve the data of an item selected from the drop down list to the textbox. There are six items in the list and for each item there has different descriptions. This is my model class

public partial class Service { public int Id { get; set; } public string ItemName { get; set; } public string Description { get; set; } }

In this ItemName are the items in the dropdown list, and description is the data for each items in the dropdown, these values are stored in the table.

controller page

public async Task<ActionResult> Edit(int? id){

if (id == null)

{return new HttpStatusCodeResult(HttpStatusCode.BadRequest);

}

Service service = await db.Services.FindAsync(id);

if (service == null)

{return HttpNotFound();

}

return View(service);

}// POST: Services/Edit/5

// To protect from overposting attacks, please enable the specific properties you want to bind to, for

// more details see http://go.microsoft.com/fwlink/?LinkId=317598.[HttpPost]

[ValidateAntiForgeryToken]

public async Task<ActionResult> Edit([Bind(Include ="Id,ItemName,Description")] Service service)

{if (ModelState.IsValid){db.Entry(service).State = EntityState.Modified;await db.SaveChangesAsync();return RedirectToAction("Index");

}

return View(service);

}

Here how can I bind the id of the drop down items in the table to the edit action method??

view page

<div class="form-group">@Html.LabelFor(model => model.ItemName, htmlAttributes: new { @class = "control-label col-md-2" })<div class="col-md-10"><p>@Html.DropDownListFor(model => model.ItemName, new SelectList(new[] { "Core Banking", "ATM", "RTGS/NEFT", "IFSC Code", "Money Transfer", "Locker Facility", "Mobile Banking (MAMBA)" }), "Select") @Html.ValidationMessageFor(model => model.ItemName, "", new { @class = "text-danger" })&emsp; <input type="submit" value="Go" class="btn btn-default" />div>div><div class="form-group">@Html.LabelFor(model => model.Description, htmlAttributes: new { @class = "control-label col-md-2" })<div class="col-md-10">@Html.TextAreaFor(model => model.Description, 8, 100, null)@Html.ValidationMessageFor(model => model.Description, "", new { @class = "text-danger" })div>div><div class="form-group"><div class="col-md-offset-2 col-md-10"><input type="submit" value="Update" class="btn btn-default" />div>div>

div>

here i want to get the data of the item to the @Html.TextArea(). Is that possible to retrieve the data when a button is pressed??

I have created a GO button on the view page and select the item from drop down list. Then how can I get the value from the table for the item that i have selected from the drop down list ??

How can I solve this issue ??


Answers (1)