I have a View that contains n number of input fields (the number will vary based on different criteria).
(我有一个包含n个输入字段的视图(该数量将根据不同的条件而有所不同)。)
the value of each input field needs to be inserted in the database in their own row.(每个输入字段的值都需要在数据库中的自己行中插入。)
My problem is that only the first input field is inserted into the database.(我的问题是只有第一个输入字段插入到数据库中。)
The controller looks like this:(控制器如下所示:)
public ActionResult Create([Bind(Include = "Id,MemberId,Rated,Rating")] Rating rating)
{
if (ModelState.IsValid)
{
db.Ratings.Add(rating);
db.SaveChanges();
}
The value of MemberId is the same for each row, but Rated and Rating will be different.
(每行的MemberId值均相同,但“额定”和“额定”将有所不同。)
My model looks like this:
(我的模型如下所示:)
[Table("Rating")]
public partial class Rating
{
public int Id { get; set; }
public int? MemberId { get; set; }
public int Rated { get; set; }
[Column("Rating")]
public int Rating { get; set; }
public virtual TeamMember TeamMember { get; set; }
}
The view looks like this:
(该视图如下所示:)
@model Teamer.Models.Rating
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Rating</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@{
int count = 0;
foreach (var item in ViewBag.MemberId)
{
string rater = Request.Cookies["rater"]["name"].ToString();
string raterId = Request.Cookies["rater"]["raterId"];
if (item.Name.ToLower() != rater.ToLower())
{
if (ViewBag.raterId != null)
{
foreach (var raterid in ViewBag.raterId)
{
<input type="hidden" name="MemberId" value="@raterid" />
}
}
<div class="col-md-10">
<label class="control-label col-md-2" for="@item.Name">@item.Name</label>
<input type="number" name="Rating-@count" value="0" />
<input type="hidden" name="Rated" value="@item.Id" />
</div>
count++;
}
}
}
</div>
<input type="hidden" name="count" value="@count" />
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
I'm guessing, I need to loop through the values one by one, but I can't get that to work, so maybe I'm way off on that.
(我猜想,我需要一个接一个地循环遍历这些值,但是我无法使它正常工作,所以也许我对此不太满意。)
ask by Lasserh translate from so