简体   繁体   中英

All properties of my model are null in post method

In my program, I have a Rank view:

<html>
<head>
    @{ViewBag.Title = "Rank";}
    <link href="https://fonts.googleapis.com/css?family=Nunito|Raleway|Rubik" rel="stylesheet">
</head>

<body>
    @model List<WebRanker.Models.MatchupModel>
    <h2>Rank your list!</h2>
    <h2 style="font-size: small">Choose your favorite of these two</h2>

    @using (Html.BeginForm())
    {
        @Html.AntiForgeryToken()

    <div id="rankradio">
        @Html.RadioButtonFor(model => model[0].SelectedItem, Model[0].FirstItem, new { @checked = "checked" })
        <label for="choice1">@Model[0].FirstItem.ItemName</label>
        <br />
        @Html.RadioButtonFor(model => model[0].SelectedItem, Model[0].SecondItem)
        <label for="choice2">@Model[0].SecondItem.ItemName</label>

        <div>
            <button class="btn btn-success btn-large" type="submit">Next</button>
            @Html.ActionLink("Cancel", "Index", "Collection", null, new { @class = "btn btn-danger btn-large" })
        </div>
    </div>
    }
</body>

and Rank get/post methods in my controller:

[HttpGet]
public ActionResult Rank(int id)
{
    var service = GetCollectionService();

    if (!TempData.Keys.Contains("matchuplist"))
    {
        TempData["matchuplist"] = service.GetMatchups(id);
    }

    return View(TempData["matchuplist"]);
}

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Rank(MatchupModel matchup)
{
    var service = GetCollectionService();
    TempData["matchuplist"] = matchup.MatchupList.Skip(1);  // Error here
    service.IncreaseItemRankingPoints(matchup.SelectedItem.ItemID);

    return View(matchup.SelectedItem.CollectionID);
}

And a model called MatchupModel:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WebRanker.Data;

namespace WebRanker.Models
{
    public class MatchupModel
    {
        public int ListID { get; set; }
        public Item FirstItem { get; set; }
        public Item SecondItem { get; set; }
        public Item SelectedItem { get; set; }
        public List<MatchupModel> MatchupList { get; set; }
    }
}

When the user selects one of the radio buttons in the view and hits submit, I want it to set the SelectedItem property of the model and then send it to the post method in my controller. For some reason, ALL of the properties for my model are null when it reaches the controller, which causes it to break when it reaches TempData["matchuplist"] = matchup.MatchupList.Skip(1); with the error System.ArgumentNullException: 'Value cannot be null. Parameter name: source' System.ArgumentNullException: 'Value cannot be null. Parameter name: source' .

I've looked everywhere and I have no idea how to fix this. I've tried using Request[string] with RadioButton() instead of RadioButtonFor() but that only stores the type of my model as a string instead of the actual model. I'd really appreciate some help, thanks!

Add a property named choice in your matchupmodel class and in the Radiobutton() put "[0].choice" instead of just "choice". Get that value in string variable and pass it to your service

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM