I have a conditional input or select list on my razor view. When I put the action directly on my form it works fine; but when I try to get form value and send it to my controller it doesn't pass the selected value on select list.
In my model
public MyItemModel()
{
AllowedQuantities = new List<SelectListItem>();
}
public int Quantity { get; set; }
public List<SelectListItem> AllowedQuantities { get; set; }
public int EnteredQuantity { get; set; }
In my view
<td class="contribute">
<form id="add-to-cart-registry-form">
@if (item.AllowedQuantities.Count > 0)
{
<select asp-for="@item.EnteredQuantity" asp-items="@item.AllowedQuantities" name="enteredquantity" class="qty-dropdown" id="qty-drp"></select>
}
else
{
<input name="enteredquantity" type="text" class="update-textbox gr-item-update-split" value="@item.EnteredQuantity" />
}
<input type="submit" value="Add to Cart" class="update-button gr-item-update-split gr-item-update-split-button" onclick="addProductCartFmRegistry('@Url.Action("AddItemsToCartFromRegistry", new { registryItemId = item.Id, shoppingCartTypeId = (int)ShoppingCartType.ShoppingCart })', '#add-to-cart-registry-form')" />
</form>
</td>
<script>
function addProductCartFmRegistry(addToCartFmRegistryUrl, formselector) {
var atcUrl = addToCartFmRegistryUrl;
var formdata = $(formselector).serialize();
if (AjaxCart.loadWaiting != false) {
return;
}
AjaxCart.setLoadWaiting(true);
$.ajax({
cache: false,
url: atcUrl,
data: formdata,
type: 'post',
success: AjaxCart.success_process,
complete: AjaxCart.resetLoadWaiting,
error: AjaxCart.ajaxFailure
});
}
</script>
and my controller
[HttpPost]
public virtual IActionResult AddItemsToCartFromRegistry(int registryItemId, int shoppingCartTypeId, IFormCollection form)
{
}
when I prepare my model I set the default enteredquantity as 1.
if it is from input it passes the right entered value to controller; but if it is select list it passes 1 instead of selected option.
It works totally fine if I use action in my form like;
<td class="contribute">
<form action="@Url.Action("AddItemsToCartFromRegistry", new { registryItemId = item.Id, shoppingCartTypeId = (int)ShoppingCartType.ShoppingCart })" method="post">
@if (item.AllowedQuantities.Count > 0)
{
<select asp-for="@item.EnteredQuantity" asp-items="@item.AllowedQuantities" name="enteredquantity" class="qty-dropdown" id="[email protected]"></select>
}
else
{
<input name="enteredquantity" type="text" value="@item.EnteredQuantity" class="update-textbox gr-item-update-split" id="[email protected]" />
}
<input type="submit" value="Add To Cart" class="update-button gr-item-update-split gr-item-update-split-button" onclick="addProductCartFmRegistry(@item.Id)" />
</form>
</td>
but I would like to do it with ajax call to return json warnings to my page.
Any ideas what is missing on ajax call version? And how can send the correct selected list item value to my controller?
I didn't mention on my question but my td was in loop.
just added @item.Id on form name and onclick function works fine.