Watch this example on YouTube
1. Model
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace WebApplication1.Models
{
public class OrderCustomClass
{
public SelectOrders_Result or { get; set; }
public IEnumerable<SelectProducts_Result> prod { get; set; }
public string SelectedValue { get; set; }
}
}
2. View
@model WebApplication1.Models.OrderCustomClass
@using (Html.BeginForm("Index", "Home"))
{
@Html.HiddenFor(x=>x.SelectedValue)
@Html.DropDownListFor(x=>x.or.ProductID, new SelectList(Model.prod,"ProductID", "Name"), new { @id = "myID" })
<input type="submit" class="submit" value="Submit" />
}
@section Scripts{
<script type="text/javascript">
$(document).ready(function () {
$(".submit").click(function (evt) {
$("#SelectedValue").val($("#myID option:selected").text());
});
});
</script>
}
3. Controller
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebApplication1.Models;
namespace WebApplication1.Controllers
{
public class HomeController : Controller
{
CompanyEntities db = new CompanyEntities();
public ActionResult Index()
{
OrderCustomClass model = new OrderCustomClass();
model.or = new SelectOrders_Result();
model.prod = db.SelectProducts();
return View(model);
}
[HttpPost]
public ActionResult Index(OrderCustomClass model)
{
model.or = new SelectOrders_Result();
model.prod = db.SelectProducts();
return View(model);
}
public ActionResult About()
{
ViewBag.Message = "Your application description page.";
return View();
}
public ActionResult Contact()
{
ViewBag.Message = "Your contact page.";
return View();
}
}
}
No comments:
Post a Comment