Home > OS >  viewbag not properly working transfer the wrong data
viewbag not properly working transfer the wrong data

Time:08-09

I am student I am new to 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 >

        <div >
            <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 >
        <div >
            <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

enter image description here

enter image description here

I am trying to pass controller warrenty not 2

see store procedure getType fill this sp in dropdown

enter image description here

I tried hiddenfor attribute but it not work

I want the pass warrenty to createcustomer controller not 2

please help

CodePudding user response:

Before trying to create code, you have to learn that the first letter in MVC is for model. So you have forget that viewbag is even exist. Create a view model , assign data and pass it from the action and use it inside of the view

  TypeViewModel.TypeList = objlistofcountrytobind;
  return View (TypeViewModel)

and you can only assign as a hidden (or not hidden) the primitive types (as string or int) not the whole instanse of the class

CodePudding user response:

You can modify your view model as described in the following post: How to get DropDownList SelectedValue in Controller in MVC.

Or you can use the JavaScript:

@Html.DropDownListFor(model => model.TypeList,
    ViewBag.type_name as SelectList,
    new { onchange=" { var ddltext = $(`#TypeList option:selected`).text();$('#textvalue').val(ddltext);}" }) 

@Html.Hidden("typeList", "")

<script src="~/Scripts/jquery-3.3.1.min.js"></script>
  • Related