Reputation: 8302
EDIT: It appears I approached this problem the wrong way. I will explain what I need to happen
Here is my model:
public class Items
{
public string ItemName { get; set; }
public string Type { get; set; }
public bool Picked { get; set; }
}
This is my controller:
public class InvoiceController : Controller
{
[HttpGet]
public ActionResult Index()
{
using (TPGEntities context = new TPGEntities())
{
List<Items> result = (from a in context.IV00101
select new Items { ItemName = a.ITEMDESC, Type = a.ITEMNMBR, Picked = false }).Take(10).ToList();
return View(result);
}
}
[HttpPost]
public ActionResult Index(string searchTerm, IList<Items> model)
{
return View();
}
}
And my view:
@model IList<SampleEnterprise.Models.Items>
@{
ViewBag.Title = "Repair Invoicing";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Repair Invoicing</h2>
@using (Ajax.BeginForm(new AjaxOptions { HttpMethod = "POST", InsertionMode = InsertionMode.Replace, UpdateTargetId = "partsList" }))
{
<input type="search" name="searchTerm" />
<input type="submit" value="OK" />
<div id="partsList">
@foreach (var item in Model)
{
<h1>@item.ItemName</h1>
<div>@item.Type</div>
<div>
@if (@item.Picked)
{
<img src="~/Images/checkbox.png" />
}
</div>
}
</div>
}
Basically here is what I need to happen:
User goes to page. In a textbox I don't have currently on my view, the user will type in a number. That number will be used to lookup and return data from the entity framework. This list of data needs to be stored temporarily somehow.
I will use the for each loop in razor to display the temp data.
In the search textbox, the user will search the temp data for an itemnumber.
If item number exits in temp data, set picked to true.
Repeat until all the picked properties are set to true.
Upvotes: 0
Views: 889
Reputation: 498
I've seen the error and all you need to add is a the hidden field in order for it to fetch the value. You can do something like @Html.HiddenFor(x => x.ItemName)
since your doing it inside the form.
Upvotes: 1
Reputation: 2230
<h1>@item.ItemName</h1>
and <div>@item.Type</div>
are simply turning into plain text (e.g., <h1>Item</h1>
) and have no binding to the model once converted to HTML. You would need to store them in something the controller action can understand, using @Html.HiddenFor(x => x.ItemName)
or creating your own HTML Helper like @Html.DivFor, for example.
Upvotes: 2
Reputation: 15619
If you wanted those fields to be returned you would need to create hidden fields for them.
@Html.HiddenFor(x => x.ItemName)
Then they would get returned as expected.
To be honest though I would probably just populate the model in the action method, as really all you want returned in the post is the search term.
Upvotes: 2
Reputation: 151720
You're not creating any input elements for the Items
, so nothing gets posted back.
Upvotes: 2