-1

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

enter image description here

enter image description here

I am trying to pass controller warranty not 2

see stored procedure getType fill this stored procedure in dropdown

enter image description here

I tried hiddenfor attribute but it not work

I want the pass warranty to createcustomer controller not 2

please help

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Developer
  • 15
  • 6

3 Answers3

1

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

Serge
  • 40,935
  • 4
  • 18
  • 45
  • can you help me more ```you have to learn that the first letter in MVC is for model.``` i am not understood this sentence – Developer Aug 09 '22 at 10:49
  • @Developer This is why you have to start from the tutorials, not from trying to create the code. You will never learn otherwise. – Serge Aug 09 '22 at 10:58
  • can you give best tutorial link to adopt the code easy way i write the logic in notebook and perform the code on laptop and facing error i am google it can you provide what should i do? – Developer Aug 09 '22 at 11:12
1

Pass text and value field same, if you want the text field to be posted back to the controller action method. By default dropdownlist uses value field. Change that line!

SelectList objlistofcountrytobind = new SelectList(dt.AsDataView(), "typename", "typename", 0);

Abdul Haseeb
  • 514
  • 4
  • 13
0

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>
Jackdaw
  • 7,626
  • 5
  • 15
  • 33