question

Maryan avatar image
0 Votes"
Maryan asked YijingSun-MSFT answered

CheckBox returns always false value

Does anyone know why I'm getting always a false value when I update the form? I tried with (value="false") into the checkbox but still doesn't work. Any help is appreciated!

<form asp-controller="Weather" asp-action="Update" method="POST">
<tr>
<td>@city.City</td>
<td><input type="number" name="cityId" value="@city.Id" hidden/></td>
<td><input type="checkbox" asp-for="@city.OnHeader" /></td>
<td><input type="checkbox" asp-for="@city.OnProfile" /></td>
<td><a asp-controller="Weather" asp-action="Delete" asp-route-cityId="@city.Id"><i class="fas fa-trash-alt color"></i></a></td>
<td><button type="submit"><i class="fas fa-edit color"></i></button></td>
</tr>
</form>

  [HttpPost]
     public IActionResult Update(WeatherModel theWeather, int cityId)
     {
         _weatherService.Update(cityId, theWeather.OnHeader, theWeather.OnProfile);
         return RedirectToAction("Settings");
     }
dotnet-aspnet-general
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Bruce-SqlWork avatar image
0 Votes"
Bruce-SqlWork answered

in the form the model is named "city" but on the action "theWeather". try:

      [HttpPost]
      public IActionResult Update(WeatherModel city, int cityId)
      {
          _weatherService.Update(cityId, city.OnHeader, city.OnProfile);
          return RedirectToAction("Settings");
      }
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

YijingSun-MSFT avatar image
0 Votes"
YijingSun-MSFT answered

Hi @Maryan,
Yes,the first way you could to change the model name in the update method. Now, there are the second way that you could add bind prefix in the method. Just like this:

 public IActionResult Update([Bind(Prefix="city")]WeatherModel theWeather, int cityId)

When you used asp-for, it will generate id and name. The name is city.property. And you could bind the name.

Best regards,
Yijing Sun


If the answer is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our  documentation  to enable e-mail notifications if you want to receive the related email notification for this thread.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.