1

can you please tell my what is wrong with my code and what am i doing wrong with my code because my post paramethar is returning null? This is my code:

[HttpGet]
public ActionResult ImeRabota()
{
    var categories = new List<IzberiRabota>();
    foreach (var category in databaseprofil.Rabotas.ToList()) 
    {
        categories.Add(new IzberiRabota
        {
            id = category.Id,
            Ime = category.Ime,
            Assigned = false 
        });
    }
    return View(categories);
}

[HttpPost]
public ActionResult ImeRabota(List<IzberiRabota> rabota)
{
    return View();
}

and this is my html view I don't know what is it:

@model IEnumerable<Rabotnik.mk.Models.IzberiRabota>
....
@using (Html.BeginForm("ImeRabota", "Profil", FormMethod.Post))
{
    @Html.AntiForgeryToken()
    @foreach (var product in Model)
    {
        @Html.CheckBoxFor(Model => product.Assigned)
        @product.Ime;
    }
    <input type="submit" value="Submit" class="btn btn-success" />
}

And this is my ViewModel

public class IzberiRabota
{
    public int id { get; set; }
    public string Ime { get; set; }
    public bool Assigned { get; set; }
}  
  • Can you show what `IzberiRabota` looks like? Also, have you checked the network tab on the developer tools to see what is being posted? – Andy T Dec 13 '16 at 17:45
  • @AndrésNava-.NET look now in the question. – Darko Arnaudov Dec 13 '16 at 17:49
  • 1
    I believe that the only property being POSTed is the `Assigned` property. I would recommend that you at least add a hidden field inside the for-loop that has the Id, so that it will also be posted. Can you take a look at your browser's developer tools > Network to see what is actually being posted? – Andy T Dec 13 '16 at 17:57
  • Probably related to using `foreach` instead of a `for` loop. http://stackoverflow.com/questions/14165632/asp-net-mvc-4-for-loop-posts-model-collection-properties-but-foreach-does-not – stephen.vakil Dec 13 '16 at 18:25

0 Answers0