The problem is very simple. I would like to populate DropDownList with values from another table. But I don't want to link them with foreigin key.
My model
public class Application1
{
public int Application1Id { get; set; }
public string ApplicationType { get; set; }
}
field ApplicationType
is the one that should keep the name of ApplicationType
(from other table)
My Controller :
// GET: Application1/Create
public ActionResult Create()
{
ViewBag.AppDataApplicationType = new SelectList(db.AppDataApplicationTypes, "AppDataApplicationTypeId", "Name");
return View();
}
// POST: Application1/Create
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Application1 application1)
{
if (ModelState.IsValid)
{
db.Application1.Add(application1);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.AppDataApplicationTypeId = new SelectList(db.AppDataApplicationTypes, "AppDataApplicationTypeId", "Name");
return View();
}
And my view :
<div class="form-group">
<div class="col-md-5">
<span class="glyphicon-asterisk" style="color:red;"></span>
@Html.LabelFor(model => model.ApplicationType , htmlAttributes: new { @class = "control-label", style="margin-bottom:10px;" })
@Html.DropDownListFor(model => model.ApplicationType, (SelectList) ViewBag.AppDataApplicationType, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.ApplicationType, "", new { @class = "text-danger" })
</div>
</div>
The problem is that when I'm trying to save this everything is ok but it's saving id instead of value.
I'll be very appreciate for any help, spend hours for trying solve it ...
Try changing
ViewBag.AppDataApplicationType = new SelectList(db.AppDataApplicationTypes, "AppDataApplicationTypeId", "Name");
to
ViewBag.AppDataApplicationType = new SelectList(db.AppDataApplicationTypes, "Name", "Name");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.