I'm having problems sending values back to my controller from a simple view in mvc.
In the view I have a simple table that gets populated on the user selecting a dropdown list value. Here is an extract of the view:
@model IList<RFQFreight.Models.TargetTransitTotal>
@using (Ajax.BeginForm("InputTimev2", "Target",
new AjaxOptions
{UpdateTargetId = "result", HttpMethod = "POST",
InsertionMode = InsertionMode.Replace}))
{
<table class="tblTarTime">
<thead>
<tr><td>Select RFQ</td></tr>
</thead>
<tr>
<td>
<select id="ddlTarTimRFQs" name="ddlTarTimRFQs">
@{
string getRfqUrlId = HttpContext.Current.Request.QueryString["rfqid"];
if (getRfqUrlId == null){getRfqUrlId = "";}
List<string> rfqLst = ViewBag.rfqsLst;
rfqLst.Sort();
}
<option value=""></option>
@foreach (var item in ViewBag.rfqsLst)
{
if (getRfqUrlId == item)
{<option value="@item" selected>@item</option>}
else{<option value="@item">@item</option>}
}
</select>
</td>
</tr>
</table>
<table id="tblTarTime" class="tblTarTime">
@if (ViewBag.rfqSelected == true)
{
<thead>
<tr>
<td>Route</td>
<td>Transit Target Time</td>
</tr>
</thead>
foreach (var item in Model)
{
<tr>
@Html.HiddenFor(model => item.rfqId)
</tr>
<tr>
<td>
@Html.DisplayFor(model => item.serviceId)
@Html.HiddenFor(model => item.serviceId)
</td>
<td>
@Html.TextBoxFor(model => item.amount)
@Html.HiddenFor(model => item.amount)
@*<input type="text" value="@item.amount" />*@
</td>
</tr>
}
<tr><td colspan="2"></td></tr>
<tr>
<td colspan="2">
@*<a href="#" class="linkSubmit" id="save">Save</a>*@
<input type="submit" value="Save" id="saveTarget" onclick="checkInputsTime(event)" />
</td>
</tr>
}
else
{
<tr><th width="400">No Routes Available</th></tr>
}
</table>
Here is an extract from the [Get] controller function:
public ActionResult InputTimev2(string rfqid = "")
{
if (rfqid == "")
{
}else{
//page load after rfqid dropdown selection
var getLstTargetTransit = new List<TargetTransitTotal>();
getLstTargetTransit = (from targetRows in db.TargetTransitTotals
join rfqs in db.RFQMasters on targetRows.rfqId equals rfqs.RfqId
where rfqs.Status != "COMPLETED"
select targetRows).ToList();
dynamic rfqLst = new List<string>();
rfqLst = (from rfq in getLstTargetTransit orderby rfq.rfqId descending select rfq.rfqId).Distinct().ToList();
ViewBag.rfqsLst = rfqLst;
ViewBag.rfqSelected = true;
var lstTargetTransit = new List<TargetTransitTotal>();
lstTargetTransit = (from targetRows in getLstTargetTransit where targetRows.rfqId == rfqid
select targetRows).ToList();
lstTargetTransit.RemoveRange(1, lstTargetTransit.Count - 1);
return View(lstTargetTransit);
}
Here is an extract from the [POST] controller function:
[HttpPost]
public ActionResult InputTimev2(List<TargetTransitTotal> model)
{
if (model != null) //This value is showing as null
{
}
}
Can anyone explain to me why the model is sent and displayed correctly in the view but when bringing it back to the controller (post) there is nothing in the model?
Thanks