视图通过列表然后如何更新帖子?

时间:2013-12-01 13:43:23

标签: asp.net-mvc

在我的GET中我有:

return View(listOfEntries);

但是当我尝试在Post中更新时,Entry对象全是0。

基本上我的视图包含该特定id的所有条目。它显示正常,但最终我希望能够编辑任何现有的Entry字段或添加一个新字段。

现在我进入POST时

  

public ActionResult Edit([Bind(Include =   “ENTRYID,用户ID,EntryQuestionId,EntryQuestion,EntryQuestionVotes,EntryAnswer,EntryReviews,   QuestionValidationURL,TopicId“)]条目入口)

条目对象没有值或0。我猜这是因为模型是一个入口对象列表,但我不知道如何才能完成我想要做的事情。

我的观点:

@model List<projInterview.Models.Entry>

@{
    ViewBag.Title = "Edit";
}

<h2>Edit</h2>


@using (Html.BeginForm())
{
    var iLoopCount = 0;
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>Entry</h4>
        <hr />
        @Html.ValidationSummary(true)
        @foreach (var item in Model)
        {
            @Html.HiddenFor(model => item.EntryId)
            <div class="form-group">
                @if(iLoopCount == 0)
                {
                    @Html.LabelFor(model => item.EntryQuestion, new { @class = "control-label col-md-2" })
                    <div class="col-md-10">
                        @Html.DisplayFor(model => item.EntryQuestion)
                    </div>
                }
                else
                {
                    <hr/>
                }
            </div>

            <div class="form-group">
                @Html.LabelFor(model => item.EntryAnswer, new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.DisplayFor(model => item.EntryAnswer)
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => item.EntryReviews, new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @{
                var iCount = 0;
                var inputName = "EntryReviews" + item.EntryId;
                for (var i = 1; i <= 5; i++)
                {
                    iCount = i;
                    if (item.EntryReviews == i)
                    {
                        <input name=@inputName type="radio" class="star" checked="checked" value="@i" />
                    }
                    else
                    {
                        <input name=@inputName type="radio" class="star" value="@i" />
                    }
                }
}
                </div>
                @*@Html.Partial("_ReviewPartial")*@
            </div>

            iLoopCount++;
            @*if (iLoopCount >= Model.Count())
            {
              <hr/>
              <div class="form-group">
                @Html.LabelFor(model => item.EntryAnswer,"Add New Answer: ", new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.TextArea("EntryAnswer",String.Empty)
                    @Html.ValidationMessageFor(model => item.EntryAnswer)
                </div>
              </div>      
            }*@
        }




        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Save" class="btn btn-default" />
            </div>
        </div>
    </div>
}


<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

我的控制器(部分):

   // GET: /Entry/Edit/5
    [Authorize]
    public ActionResult Edit(int? id)
{
    if (id == null)
    {
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
    }

    Entry entry = db.Entries.Find(id);
    List<Entry> listOfEntries = new List<Entry>();
    listOfEntries = db.Entries.Where(e => e.EntryQuestionId == id).ToList();
    if (listOfEntries == null)
    {
        return HttpNotFound();
    }
    ViewBag.TopicId = new SelectList(db.Topics, "TopicId", "TopicName", entry.TopicId);
    return View(listOfEntries);
}

// POST: /Entry/Edit/5
// To protect from overposting attacks, please enable the specific properties you want to bind to, for 
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
[Authorize]
public ActionResult Edit([Bind(Include = "EntryId,UserId,EntryQuestionId,EntryQuestion,EntryQuestionVotes,EntryAnswer,EntryReviews,QuestionValidationURL,TopicId")] Entry entry)
{
    if (ModelState.IsValid)
    {
        EntryAnswerReview myReview = new EntryAnswerReview();
        myReview.EntryId = entry.EntryId;
        myReview.EntryAnswerReviewValue = entry.EntryReviews;
        myReview.DateReviewed = DateTime.Now;
        myReview.UserId = User.Identity.GetUserId();
        db.Reviews.Add(myReview);

        List<Int32> reviews = new List<Int32>();
        reviews = db.Reviews.Where(x => x.EntryId == entry.EntryId).Select(x => x.EntryAnswerReviewValue).ToList();
        Double average;
        if (reviews.Count == 0)
        {
            average = entry.EntryReviews;
        }
        else
        {
            average = reviews.Average();
        }


        entry.EntryReviews = Convert.ToInt32(average);

        entry.UserId = User.Identity.GetUserId();
        if (entry.UserId == null)
        {
            throw new InvalidOperationException("User [" +
                User.Identity.Name + " ] not found.");
        }
        entry.LastUpdateDate = DateTime.Now;
        db.Entry(entry).State = EntityState.Modified;
        db.SaveChanges();
        return RedirectToAction("Index");
    }
    ViewBag.TopicId = new SelectList(db.Topics, "TopicId", "TopicName", entry.TopicId);
    return View(entry);
}

进一步澄清我想要做的事情:

让我们说get上的编辑视图返回2条目项:

  • 条目#1

  • 条目#2

我希望能够从这个视图中更新其中任何一个。

但我也希望有一个“新条目”选项(现在只是答案的文本区域。所以在我的控制器中如果填写完了,我希望能够捕获到一个新条目并且将其添加到数据库。

0 个答案:

没有答案