-1

In my application, it displays a certain amount of items depending on each user. Let's just say, a user logs in and see 3 books and there are radio buttons for each item that indicate "Buy" or "Rent" that they can choose or populates preset value already selected. I've assigned the value true or false for each radio button.

So here, I'm trying to POST the value of each radio button, but I'm not getting anything back from the radio buttons when I step through. Everything else comes back except the value from the radio buttons. I've tried "FormCollection", Retrieving from the ViewModel and Request.Form[], but none work.

Down below is a sample code:

ViewModel

public class CustomerBooksViewModel
{
    public int? ID {get; set;}
    public int? CustomerID {get;set;}
    public string Title {get;set;}
    public string Author {get;set;}
    public string ISBN {get;set;}
    public bool? BuyRent{get;set;}
}

Controller - Request.Form Example

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Update()
{
    var ItemCustomerID = Request.Form["item.CustomerID"];
    var ItemTitle  = Request.Form["item.Title "];
    var ItemAuthor  = Request.Form["item.Author "];
    var ItemISBN  = Request.Form["item.ISBN "];
    var ItemBuyRent = Request.Form["item.BuyRent"];
}

View - (Just a sample for the radio buttons)

@using(Html.BeginForm("Update", "CustomerBooks", FormMethod.Post))
{
    @Html.AntiForgeryToken()
    @foreach (...)
    {
      ...
      <td>
      @Html.RadioButtonFor(modelItem => item.BuyRent, true, new{ @Name = "", id=""})) Buy
      @Html.RadioButtonFor(modelItem => item.BuyRent, false, new{ @Name = "", id = ""})) Rent
      </td>
      ...
    }
}
Kluong
  • 302
  • 3
  • 15
  • What are you looping over in the `foreach`? The value selected for the radio button should be in `CustomerBooksViewModel.BuyRent` in the post. [This](https://stackoverflow.com/a/3122130/4228458) can be helpful too. – CodingYoshi Feb 22 '18 at 02:21
  • In addition to the dupe, since `new { @Name = "" }` essentially removes the `name` attribute, then there is nothing posted back (never, under any circumstances, attempt to modify the `name` attribute which will always be correctly generated by the `HtmlHelper` methods) –  Feb 22 '18 at 02:31
  • And since the `BuyRent` property is nullable, you need an additional radio button for `null` (or make the property not nullable) –  Feb 22 '18 at 02:32
  • I'm looping through a number of books that the user have. so if there's 3 books for that customer, it will display 3 books. There's a linq query in my controller that I'm making a call to the DB to grab the data, loop through to add the data into a list and display that list in the view. – Kluong Feb 22 '18 at 02:32
  • @Kluong, Read the dupe!!. You **cannot** use a `foreach` –  Feb 22 '18 at 02:33
  • @StephenMuecke Calm down, I was just answering CodingYoshi question. Thanks for the help, I'll look into it. – Kluong Feb 22 '18 at 02:35
  • @kluong I asked because when you use a `foreach`, the controls created for all books will have the same `name` and `id` and that could be the reason why your post is not working. Read the dup because it has more details. You can also refer [this](https://stackoverflow.com/a/48477658/4228458) and [this](https://stackoverflow.com/a/48357541/4228458) to fully understand what I mean. – CodingYoshi Feb 22 '18 at 02:44
  • @CodingYoshi, Yeah i'm reading the dup, but couldn't you just give a different name and id by just modifying the this: name = item.ISBN and id = item.ISBN. So each book will have a different name and id every time it loops – Kluong Feb 22 '18 at 02:49
  • @kluong you can but it has to be a specific format and knowing the format is hard and unnecessary. It is best to let mvc generate that for you. Less code means less work for you. Please carefully read the last link in my comment above. – CodingYoshi Feb 22 '18 at 02:53

1 Answers1

-1

First of all you should utilize Model Binder unless you have a valid reason not to. So, your code should be:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Update(CustomerBooksViewModel model)
{
   // parameter model should be filled automatically with all properties from request
}

Now, regarding the property BuyRent it will not be posted in the request since you already overriding the name and id to empty strings.

It should be:

 @Html.RadioButtonFor(modelItem => item.BuyRent, true) Buy
 @Html.RadioButtonFor(modelItem => item.BuyRent, false) Rent
Haitham Shaddad
  • 4,336
  • 2
  • 14
  • 19