我正在使用MVC .NET 4.5,我的表单返回null而不是List对象。同时我有这个Jquery函数,它将HTML添加到表单中,但它不只是添加HTML,它使表单POST到控制器。我不知道这两者是否相关,但以防我提供所有相关代码。请帮忙。
模特课:
public class BidModels
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id{ set; get;}
//Who put the bid...
public string UserId { set; get; }
//The Compnay putting the bid
[ForeignKey("CompanyModels")]
public int? CompanyId { set; get; }
public virtual CompanyModels CompanyModels { set; get; }
//The position being bid on
[ForeignKey("ProjectPositionModels")]
public int? PositionId { set; get; }
public virtual ProjectPositionModels ProjectPositionModels { set; get; }
//How much?
[DataType(DataType.Currency)]
public int? BidAmount { set; get; }
}
控制器:
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> AddBid(IList<BidModels> bids)
{
//Iterate the list for every new bid in it.
var userId = User.Identity.GetUserId();
var companyId = db.CompanyModels.Single(x => x.UserId == userId);
//checking for content
Console.Write(bids.ToString());
//db.BidModels.Add();
// await db.SaveChangesAsync();
return View("Index");
}
局部视图:
<form method="post" action="/BidModels/AddBid">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title">Make a Bid</h4>
</div>
<div class="modal-body">
<div class="row">
<div style="margin-left:15px;">
<button id="add"><span class="glyphicon glyphicon-plus"></span></button>
<button id="remove"><span class="glyphicon glyphicon-minus"></span></button>
</div>
@Html.AntiForgeryToken()
<table class="table" id="form-table">
<tr>
<th>
<strong>Position</strong>
</th>
<th>
<strong>Bid Amount</strong>
</th>
</tr>
<tr class="form-row">
<td>
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<input type="hidden" name="bid[0].Id" value="" />
<input type="hidden" name="bid[0].UserId" value="" />
<input type="hidden" name="bid[0].CompanyId" value="" />
@Html.DropDownList("bid[0].PositionId", (IEnumerable<SelectListItem>)ViewBag.SelectList, htmlAttributes: new { @class = "form-control dlist" })
</td>
<td>
<div class="input-group">
<span class="input-group-addon">$</span>
<input type="text" class="form-control" name="bid[0].BidAmount" />
<span class="input-group-addon ">.00</span>
</div>
</td>
</tr>
</table>
</div>
</div>
<div class="modal-footer">
<button type="button" data-dismiss="modal" class="btn btn-default">Close</button>
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</form>
Jquery的:
$(document).ready(function () {
$i = 0;
var bid = $("#form-table");
var dlist = $(".dlist").clone().attr("name", "bid[" + $i + "].PositionId");
$("#add").click(function () {
$i++;
var id = '<input type="hidden" name="bid[' + $i + '].Id"/>';
var userId = '<input type="hidden" name="bid[' + $i + '].UserId"/>';
var companyId = '<input type="hidden" name="bid[' + $i + '].CompanyId"/>';
var bidAmount = '<div class="input-group"><span class="input-group-addon">$</span><input type="text" class="form-control" name="bid[' + $i + '].BidAmount"/></div>';
bid.append("<tr><td id='mvc" + $i +"'>" + id + userId + companyId + "</td><td> " + bidAmount + " </td></tr>");
$("#mvc" + $i + "").append(dlist.clone());
});
$("#remove").click(function () {
if ($("#form-table tr").length > 2) {
$i--;
$("#form-table tr:last").remove();
} else {
return;
}
});
});
答案 0 :(得分:1)
您使用的名称为bid
,但您的控制器正在使用bids
。尝试为操作bid
创建参数名称(反之亦然)并再次运行。