Asp.net dynamic dropdownlist access with jquery - c#

I am creating a dynamic dropdownlist in asp.net which is under asp:listview control code is as below:
<asp:ListView runat="server" ID="lst" OnItemDataBound="lst_dataBound">
<ItemTemplate>
<asp:DropDownList runat="server" CssClass="test" ID="ddl"></asp:DropDownList>
<asp:HiddenField runat="server" ID="hf" Value='<%# Eval("Id") %>' />
<a class="btn btn-success" id='<%#Eval("ID") %>' onclick="Add('<%#Eval("ID")%>')>Add</a>
</ItemTemplate>
</asp:ListView>
on the server side I have given source and bind the list and on item databound event I have bound dropdownlist which is perfectly but as shown above when I click on an anchor tag I want selected item from drop down for that I have tried this:
var item = $('.test option:selected').text();
alert(item);
which is giving me all list items like if i have 10 list so 10 dropdowns so if each have 4 items take text as(1,2,3,4)
selected item at each list so randomly it will ("1212134232") which is 10 item in alert but I want single one so I tried another solution I give dynamic cssclass at server side like this
DropDownList myDDL = (DropDownList)e.Item.FindControl("ddl");
HiddenField HF_ID = (HiddenField)e.Item.FindControl("hf");
myDDL.ID = "test" + HF_ID.Value;
so now I have dynamic class name I can access them at jQuery - JavaScript
so code which I use them for is
here id is which I will get from function parameter
var test = "test" + id;
var item = $('.test option:selected').text();
alert(item);
here it should give the right answer but it is not giving it so I try further I inspect element and see if this working and yes its working dynamic css class they have so I have think further and tried like if dynamic side name given like
test20, test21, etc...
y not try static value so I tried
var item = $('.test20 option:selected').text();
alert(item);
and yes its working it is giving me specific dropdown list selected value but this can't be done for statically so I needed help here.
rendered html is:
<select class="test29" id="ContentPlaceHolder1_lst_ddl_5" name="ctl00$ContentPlaceHolder1$lst$ctrl5$ddl">
<option value="85">1</option>
<option value="86">2</option>
<option value="87">3</option>
<option value="88">4</option>
</select>
screen shot of clicking element alert dialog which should give test29(1234(one of them))
but getting....
<div class="col-xs-12 col-ms-6 col-sm-4 col-md-3 col-lg-3">
<select name="ctl00$ContentPlaceHolder1$lst$ctrl0$ddl" id="ContentPlaceHolder1_lst_ddl_0" class="test23">
<option value="65">1</option>
<option value="66">2</option>
<option value="67">3</option>
<option value="68">4</option>
</select>
<input type="hidden" name="ctl00$ContentPlaceHolder1$lst$ctrl0$hf" id="ContentPlaceHolder1_lst_hf_0" value="23">
<a class="btn btn-success" id="23" onclick="Add('23')">
Add</a>
</div>
<div class="col-xs-12 col-ms-6 col-sm-4 col-md-3 col-lg-3">
<select name="ctl00$ContentPlaceHolder1$lst$ctrl1$ddl" id="ContentPlaceHolder1_lst_ddl_1" class="test25">
<option value="69">1</option>
<option value="70">2</option>
<option value="71">3</option>
<option value="72">4</option>
</select>
<input type="hidden" name="ctl00$ContentPlaceHolder1$lst$ctrl1$hf" id="ContentPlaceHolder1_lst_hf_1" value="25">
<a class="btn btn-success" id="25" onclick="Add('25')">
Add </a>
</div>
<div class="col-xs-12 col-ms-6 col-sm-4 col-md-3 col-lg-3">
<select name="ctl00$ContentPlaceHolder1$lst$ctrl2$ddl" id="ContentPlaceHolder1_lst_ddl_2" class="test26">
<option value="73">1</option>
<option value="74">2</option>
<option value="75">3</option>
<option value="76">4</option>
</select>
<input type="hidden" name="ctl00$ContentPlaceHolder1$lst$ctrl2$hf" id="ContentPlaceHolder1_lst_hf_2" value="26">
<a class="btn btn-success" id="26" onclick="Add('26')">
Add</a>
</div>
<div class="col-xs-12 col-ms-6 col-sm-4 col-md-3 col-lg-3">
<select name="ctl00$ContentPlaceHolder1$lst$ctrl3$ddl" id="ContentPlaceHolder1_lst_ddl_3" class="test27">
<option value="77">S</option>
<option value="78">M</option>
<option value="79">L</option>
<option value="80">XL</option>
</select>
<input type="hidden" name="ctl00$ContentPlaceHolder1$lst$ctrl3$hf" id="ContentPlaceHolder1_lst_hf_3" value="27">
<a class="btn btn-success" id="27" onclick="Add('27')">
Add</a>
</div>
<div class="col-xs-12 col-ms-6 col-sm-4 col-md-3 col-lg-3">
<select name="ctl00$ContentPlaceHolder1$lst$ctrl4$ddl" id="ContentPlaceHolder1_lst_ddl_4" class="test28">
<option value="81">1</option>
<option value="82">2</option>
<option value="83">3</option>
<option value="84">4</option>
</select>
<input type="hidden" name="ctl00$ContentPlaceHolder1$lst$ctrl4$hf" id="ContentPlaceHolder1_lst_hf_4" value="28">
<a class="btn btn-success" id="28" onclick="Add('28')">
Add</a>
</div>
any other info needed?

You can try below code to get the selected value of dropdown in same list item. you need to write it in click event handler so that this will give the invoker object and that will help to find the nearest dropdown in same list item.
$(this).siblings("select").find(":selected").val()

Related

ASP.NET core MVC how to use dropdown list

Im trying to make dropdown list in my razor view using Viewbag but its not working, Im missing something I guess
public IEnumerable<SelectListItem> GetAllEmployeeForPayroll()
{
return GetAll().Select(emp => new SelectListItem()
{
Text = emp.FullName,
Value = emp.Id.ToString()
});
}
ViewBag.employees = _employeeService.GetAllEmployeeForPayroll();
<label asp-for ="EmployeeId"></label>
<select asp-for="EmployeeId" class="form-control" asp-item="ViewBag.employees">
<option selected >---Select Employee---</option>
</select>
<span asp-validation-for ="EmployeeId" class="text-danger"> </span>
this is my code Im sure I did something wrong in razor view but cant figure out what.enter code here
I hope this answer your question regards.
<div class="row">
<div class="col-md-4">
<select id="drpEmpList" class="form-control" asp-for="EmployeeId" asp-items="#ViewBag.employees">
<option value="">--Select--</option>
</select>
<input type="hidden" asp-for="EmployeeId" />
</div>
It should be asp-items but not asp-item.
<select asp-for="EmployeeId" class="form-control" asp-items="ViewBag.employees">
<option selected >---Select Employee---</option>
</select>

How to send dynamic amount of HTML forms to controller as list of objects?

I am doing a .NET Core MVC project. In a partial view, I want some information from user to fill as HTML form. That information is variables of a model, so every filled list is an object. After user filled the objects, I want to send that object to my controller as a List of that object. User may send 3 forms or 5 forms or 1 form. My question is, when it is only 1 form to send, everything was ok, I gave variables names' of model as name attribute to my HTML inputs in a form tag and my object was filling. But in that case which I don't know how many objects will come, I could not figure out how can I get list of objects. I hope it is clear.
Here is my View:
#model List<EpubAdmin.Models.KyProducts>
#{
ViewData["Title"] = "AddBook";
Layout = null;
}
#foreach(var product in Model)
{
<div class="col col-xl-12">
<div class="row justify-content-center">
<div class="col-xl-5 col-lg-5 ">
<form method="post" action="/File/AddProductFile" enctype="multipart/form-data" id="myForm">
<br />
<b>Kitap Adı: </b>
<input class="form-control" value="#(Html.Raw(product.Name))" name="Name"/>
<br />
<b>Sayfa Sayısı: </b>
<input class="form-control" value="#product.PageNumber" name="PageNumber" />
<br />
<b>Product Id: </b>
<input class="form-control" value="#product.KyProductId" name="KyProductId" readonly />
<br />
<b>Barkod: </b>
<input class="form-control" value="#product.Barcode" name="Barcode" readonly />
<input class="form-control" value="#product.Author" name="Author" hidden />
<input class="form-control" value="#product.ImageId" name="ImageId" hidden />
<input class="form-control" value="#product.PublisherName" name="PublisherName" hidden />
<input class="form-control" value="#product.PublisherId" name="PublisherId" hidden />
<br />
#*<select class="selectpicker" data-live-search="true" data-show-subtext="true">
<option value="0">Tarih</option>
<option value="1">Çocuk</option>
<option value="2">Psikoloji</option>
<option value="3">Kültür</option>
</select>*#
#if (product.IsDuplicate)
{
<div class="alert alert-danger">
<p>* Bu kitap zaten ekli.</p>
</div>}
else
{
<h5>Kategori Seç</h5>
<select class="form-select ml-0" data-live-search="true">
<option selected>Tarih</option>
<option value="1">Çocuk</option>
<option value="2">Psikoloji</option>
<option value="3">Kültür</option>
</select>
<select class="form-select ml-5">
<option selected>Tarih</option>
<option value="1">Çocuk</option>
<option value="2">Psikoloji</option>
<option value="3">Kültür</option>
</select>
<select class="form-select ml-5">
<option selected>Tarih</option>
<option value="1">Çocuk</option>
<option value="2">Psikoloji</option>
<option value="3">Kültür</option>
</select>
<br />
<br />
<label for="file">Yüklenecek kitabı seçin.</label>
<input type="file" class="form-control-file" id="File" accept=".epub" name="File">
}
</form>
<br />
<br />
</div>
</div>
<br />
<hr />
<br />
</div>
}
<div class="float-right">
<button class="btn btn-info w-100" onclick="onclk()">Ekle</button>
</div>
<br />
<br />
<script>
function onclk() {
$("#myForm").submit();
alert("a");
}
</script>
Controller:
[HttpPost]
public IActionResult AddProductFile(List<KyProducts> model)
{
foreach(var product in model)
{
if (product.file == null || product.KyProductId == 0 || product.PublisherId == 0)
{
return RedirectToAction("List", "Books");
}
}
}
I thought about using for instead of foreach in my view so I can get indexes and can name my HTML attributes as name="id+#i" but that did not seem best practice to me and wasn't sure if it works. I also thought about putting hidden submit buttons for every form and on click of main submit button, triggering all hidden submit buttons but that didn't seem to be best practice again... What are your ideas?
If you want to pass a list to your action,try to put the form outside foreach,and change the name of inputs:
#model List<EpubAdmin.Models.KyProducts>
#{
ViewData["Title"] = "AddBook";
Layout = null;
}
<form method="post" action="/File/AddProductFile" enctype="multipart/form-data" id="myForm">
#{ var i = 0;}
#foreach (var product in Model)
{
<div class="col col-xl-12">
<div class="row justify-content-center">
<div class="col-xl-5 col-lg-5 ">
<br />
<b>Kitap Adı: </b>
<input class="form-control" value="#(Html.Raw(product.Name))" name="model[#i].Name" />
<br />
<b>Sayfa Sayısı: </b>
<input class="form-control" value="#product.PageNumber" name="model[#i].PageNumber" />
<br />
<b>Product Id: </b>
<input class="form-control" value="#product.KyProductId" name="model[#i].KyProductId" readonly />
<br />
<b>Barkod: </b>
<input class="form-control" value="#product.Barcode" name="model[#i].Barcode" readonly />
<input class="form-control" value="#product.Author" name="model[#i].Author" hidden />
<input class="form-control" value="#product.ImageId" name="model[#i].ImageId" hidden />
<input class="form-control" value="#product.PublisherName" name="model[#i].PublisherName" hidden />
<input class="form-control" value="#product.PublisherId" name="model[#i].PublisherId" hidden />
<br />
#if (product.IsDuplicate)
{
<div class="alert alert-danger">
<p>* Bu kitap zaten ekli.</p>
</div>}
else
{
<h5>Kategori Seç</h5>
<select class="form-select ml-0" data-live-search="true">
<option selected>Tarih</option>
<option value="1">Çocuk</option>
<option value="2">Psikoloji</option>
<option value="3">Kültür</option>
</select>
<select class="form-select ml-5">
<option selected>Tarih</option>
<option value="1">Çocuk</option>
<option value="2">Psikoloji</option>
<option value="3">Kültür</option>
</select>
<select class="form-select ml-5">
<option selected>Tarih</option>
<option value="1">Çocuk</option>
<option value="2">Psikoloji</option>
<option value="3">Kültür</option>
</select>
<br />
<br />
<label for="file">Yüklenecek kitabı seçin.</label>
<input type="file" class="form-control-file" id="File" accept=".epub" name="File">
}
<br />
<br />
</div>
</div>
<br />
<hr />
<br />
</div>
i++;
}
<input type="submit" value="submit"/>
</form>
So that when you click the button,it will pass the whole list to action.

ASP.NET Core SelectList not binding if value is string

ViewData["ComponenteId"] = new SelectList(await GetComponentes(Geometria.MatrizId), "Nome", "Nome", Geometria.Componente);
here is the view
<div class="form-row">
<div class="form-group col-md-4">
<label asp-for="Geometria.Componente" class="control-label"></label>
<select asp-for="Geometria.Componente" class="custom-select" asp-items="ViewBag.ComponenteId">
<option value="">Selecione um Componente</option>
</select>
<span asp-validation-for="Geometria.Componente" class="text-danger"></span>
</div>
</div>
no matter what it won't bind and i don't understand why when basically the value and text are the same property.
this is how it is rendered in html
<select class="custom-select input-validation-error" data-val="true" data-val-required="The Componente POE field is required." id="Geometria_Componente" name="Geometria.Componente" aria-describedby="Geometria_Componente-error" aria-invalid="true">
<option value="">Selecione um Componente</option>
<option value="M130291">M130291</option>
<option value="M130559">M130559</option>
<option value="M140004">M140004</option>
<option value="M140040">M140040</option>
<option value="SA00367A300">SA00367A300</option>
<option value="SS98940A100">SS98940A100</option>
</select>
UPDATE
It seems the reason it was not binding it is because the value had whitespaces even before i started using Entity Framework Core. The old data type was varchar hence why it was not recognizing it.

Select Tag Option Value not passing to url parameter on iOS

I have a set of html select tags that serve to accomplish a a set of search parameters for a vehicle catalogue.
The html is as follows:
<div class="clearfix select-form padding-bottom-50">
<div class="my-dropdown min-years-dropdown max-dropdown">
<select name="year" class="css-dropdowns" tabindex="1" id="txtYear" runat="server" >
<option value="">Search by Year</option>
<option value="2018">2018</option>
<option value="2017">2017</option>
<option value="2016">2016</option>
<option value="2015">2015</option>
<option value="2014">2014</option>
<option value="2013">2013</option>
<option value="2012">2012</option>
<option value="2011">2011</option>
<option value="2010">2010</option>
<option value="2009">2009</option>
<option value="2008">2008</option>
<option value="2007">2007</option>
<option value="2006">2006</option>
<option value="2005">2005</option>
<option value="2004">2004</option>
</select>
</div>
<div class="my-dropdown min-years-dropdown max-dropdown">
<select name="make" class="css-dropdowns" tabindex="1" id="txtMake" runat="server" >
<option value="">Search by Make</option>
<option value="BMW">BMW</option>
<option value="FORD">FORD</option>
</select>
</div>
<div class="my-dropdown min-years-dropdown max-dropdown">
<select name="transmission" class="css-dropdowns" tabindex="1" id="txtTrans" runat="server" >
<option value="">Search by Transmission</option>
<option value="Automatic">Automatic</option>
<option value="Manual">Manual</option>
</select>
</div>
<div class="my-dropdown min-years-dropdown max-dropdown">
<select name="price" class="css-dropdowns" tabindex="1" id="txtPrice" runat="server" >
<option value="">Search by Price</option>
<option value="50000">< R50,000</option>
<option value="100000">< R100,000</option>
<option value="150000">< R150,000</option>
<option value="200000">< R200,000</option>
</select>
</div>
<input type="button" value="Search" class="pull-left btn-inventory margin-bottom-none md-button" runat="server" id="btnSearch" onserverclick="btnSearch_ServerClick1" />
</div>
</div>
<div class="clearfix"></div>
</div>
On Codebehind:
protected void btnSearch_ServerClick1(object sender, EventArgs e)
{
Response.Redirect("usedsearch.aspx?make=" + txtMake.Value + "&year=" + txtYear.Value + "&trans=" + txtTrans.Value + "&price=" + txtPrice.Value);
}
When browsing the site on an iphone, the values from the select tags are not passed to the URL Parameters(Parameter value is blank -> eg www.....com?Make=&Year=...).
However, on an android device, it works perfectly. Really confused.

HTML Form submits empty (yes I have names) C#

Using C# with Razor syntax
I cannot figure out what is wrong...
I have a simple HTML form (I'm going to convert it to HTML Helpers soon). I have a form and within the form I have two <select> elements that contain names. For some reason the data is posting, but without any parameters
HTML
<form method="post" action="~/modules/parts/bom_ervd.cshtml">
<div class="row">
<div class="col-md-12 col-xs-12 col-sm-12">
<div class="col-md-3">
<div class="input-group">
<label>Unit Size<span class="req">*</span></label>
<select class="form-control isReq" name="ervd_unitsize" id="sel_ervd_custom_unitsize">
<option value="">--Select--</option>
<option value="101" disabled>Box 1 (1,500 to 3,000CFM)</option>
<option value="102">Box 2 (2,000 to 4,800CFM)</option>
<option value="103">Box 3 (3,500 to 7,500CFM)</option>
<option value="104">Box 4 (5,000 to 10,000CFM)</option>
</select>
</div>
</div>
<div class="col-md-3">
<div class="input-group">
<label>Power<span class="req">*</span></label>
<select class="form-control isReq" name="ervd_power" id="sel_ervd_custom_power">
<option value="">--Select--</option>
<option value="208/3/60" data-val="7" data-typ="0">208/3/60</option>
<option value="230/3/60" data-val="12" data-typ="0">230/3/60</option>
<option value="460/3/60" data-val="20" data-typ="1">460/3/60</option>
<option value="575/3/60" data-val="23" disabled>575/3/60</option>
</select>
</div>
</div>
<div class="col-md-3"></div>
<div class="col-md-3"></div>
</div>
<div id="form_subcomponents">
<div class="col-md-12">
<hr class="hr1" style="margin-top:40px !important;" />
<input class="btn-block btn btn-info" type="submit" id="btn_ervd_custom_submit" value="Submit"/>
</div>
</div><!-- end form sub components-->
</div><!-- end row -->
</form><!-- end form -->
Here's the code for page: bom_ervd.cshtml
#{
if (isPost) {
if (Request.Form["ervd_unitsize"] == null) {
/* this is what I'm seeing */
<p>I'm getting null values</p>
}
else {
<p>The value is: #Request.Form["ervd_unitsize"].ToString()</p>
}
}
else {
<p>No Data posted!</p>
}
}
In this scenario I ensure I select an option that has a value
I have also tried Request["ervd_unitsize"] and still get null values
Why is it Posting but getting null values?
What am I doing wrong?

Categories