如何在同一页面ASP.NET MVC中显示经过验证的表单和表

时间:2016-03-02 21:32:26

标签: c# asp.net asp.net-mvc html.actionlink

我正在尝试同时显示两者:验证表单和同一页面上的数据库表。代码如下:

创建控制器

<div class="wrapper">
  <article>
    <rzslider rz-slider-model="rangeSlider.minValue" rz-slider-high="rangeSlider.maxValue" rz-slider-options="rangeSlider.options"></rzslider>
  </article>
</div>

创建视图

private object charity;

  // GET: Charities/Create
    public ActionResult Create()
    {

        return View(charity);  //Display 5 recent records from table 
    }


    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create([Bind(Include = "ID,DisplayName,Date,Amount,TaxBonus,Comment")] Charity charity)
    {

        if (ModelState.IsValid)
        {
            db.Donations.Add(charity);
            db.SaveChanges();
            return RedirectToAction("AdditionalInfo");
        }
        return View(charity);
    }


    public ActionResult ChildList()
    {

        return View(db.Donations.OrderByDescending(x => x.ID).Take(5).ToList());
    }

型号

 @model CharitySite.Models.Charity
@{
            ViewBag.Title = "Create";

     }

        <link href="@Url.Content("~/Content/Donate-Style.css")" rel="stylesheet" type="text/css" />

          <div>  @Html.Partial("_Form", Model);</div>

          <div> @Html.Action("ChildList");</div>

            <div class="donatebtn">
                @Html.ActionLink("Donate", "Additionalinfo")
            </div>

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

当前错误消息

public class Charity
{
    public int ID { get; set; }
    public string DisplayName { get; set; }
    public DateTime Date { get; set; }

    [Range(2, Int32.MaxValue, ErrorMessage = "The value must be greater than 2")]
    public Double Amount { get; set; }
    public Double TaxBonus { get; set; }
    public String Comment { get; set; }
}

public class CharityDBContext : DbContext //controls information in database 
{
    public DbSet<Charity> Donations { get; set; } //creates a donation database



}

UPDATE-- 我创建了一个方法,它返回表列表并使用HTML.ACTION列表在View上显示。显示上述错误。

1 个答案:

答案 0 :(得分:0)

你的第二个Create(...)方法需要一个httpPost

[ValidateAntiForgeryToken]
[HttpPost]
public ActionResult Create([Bind(Include = "ID,DisplayName,Date,Amount,TaxBonus,Comment")] Charity charity)
{
..
}