I am student. I am new to ASP.NET MVC and I google it and I tried see I write all code but viewbag not properly working
I am transferring data and using the viewing but not transferring the dropdown value
type.cs
public class Type
{
//public int Value { get; set; }
//public string Text { get; set; }
public int typeid { get; set; }
public string typename { get; set; }
}
public class TypeViewModel
{
//public List<Type> TypeDetaills { get; set; }
public SelectList TypeList { get; set; }
}
HomeControlle.cs
TypeViewModel TypeViewModel = new TypeViewModel();
public ActionResult Index()
{
SqlCommand cmd = new SqlCommand("getType", cn);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
cn.Open();
da.Fill(ds);
DataTable dt = ds.Tables[0];
List<Type> objcountry = new List<Type>();
SelectList objlistofcountrytobind = new SelectList(dt.AsDataView(), "typeid", "typename", 0);
TypeViewModel.TypeList = objlistofcountrytobind;
ViewBag.typename = TypeViewModel.TypeList;
cn.Close();
return View();
}
[HttpPost]
public ActionResult CreateCustomer(Customer customer,string TypeList)
{
customer.Type = TypeList;
customer.CustomerName = cust;
return RedirectToAction("Index");
}
Index.cshtml
@model projectname.Models.TypeViewModel
@{
ViewBag.Title = "Index";
//var t = ViewBag.typename;
}
<h2>Type Query</h2>
@using (Html.BeginForm("CreateCustomer", "Home", FormMethod.Post, new { TypeList = @ViewBag.typename }))
{
<div class="form-group">
<div class="row">
<label>Type Name:</label>
@Html.DropDownListFor(model => model.TypeList, ViewBag.typename as SelectList)
@*@Html.Hidden("TypeList", @ViewBag.typename);*@
@*@Html.HiddenFor("TypeList", @ViewBag.typename);*@
@*@Html.HiddenFor(x => x.TypeList)*@
@*<input type="hidden" value="@ViewBag.typename" />*@
@*@Html.DropDownList("typeid", t as SelectList)*@
@*@Html.DropDownListFor(x => x.typename, new SelectList((IEnumerable<Type>)t, "typeid", "typename"))*@
</div>
</div>
<div class="form-group">
<div class="row">
<label>Customer Name:</label>
<input type="text" id="cust" name="cust" />
</div>
</div>
<input type="submit" />
}
see i select the runtime warranty
from the drop down
I am trying to pass controller warranty
not 2
see stored procedure getType
fill this stored procedure in dropdown
I tried hiddenfor attribute but it not work
I want the pass warranty
to createcustomer
controller not 2
please help