【发布时间】:2016-03-08 18:17:29
【问题描述】:
我正在尝试将用户在“创建”视图中输入的详细信息转移到另一个视图“确认”代码如下:
创建操作结果
public ActionResult Create()
{
return View(new Charity());
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "ID,DisplayName,Date,Amount,Comment")] Charity charity)
{
if (ModelState.IsValid)
{
if (!string.IsNullOrEmpty(charity.Comment))
{
var comment = charity.Comment.ToLower().Replace("hot", "###").Replace("cold", "###").Replace("Slow", "###").Replace("enjoy", "###").Replace("BAD", "###");
charity.Comment = comment; //Replaces textx from model variable - comment
charity.TaxBonus = 0.20 * charity.Amount;
}
if (string.IsNullOrEmpty(charity.DisplayName))
{
charity.DisplayName = "Annonymus"; //If user doesnt enter name then Annonymus
}
db.Donations.Add(charity);
db.SaveChanges();
return RedirectToAction("Confirmation", "Charities" , new { id = charity.ID });
}
return View(charity);
}
创建视图
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Charity</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.DisplayName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.DisplayName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.DisplayName, "", new { @class = "text-danger" })
</div>
</div>b
<div class="form-group">
@Html.LabelFor(model => model.Amount, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Amount, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Amount, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.TaxBonus, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.TaxBonus, new { htmlAttributes = new { @class = "form-control" , @readonly = "readonly" } }
)
@Html.ValidationMessageFor(model => model.TaxBonus, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Comment, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Comment, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Comment, "", new { @class = "text-danger" })
</div>
</div>
<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>
AdditionalInfo ActionResult
[HttpGet]
public ActionResult Additionalinfo(int id)
{
return View("Additionalinfo", new { id });
}
附加信息视图
@{
ViewBag.Title = "Additionalinfo";
}
<h2>Additionalinfo</h2>
型号
public class Charity
{
public int ID { get; set; }
[RegularExpression(@"^[a-zA-Z]+$", ErrorMessage = "Use letters only please")]
public string DisplayName { get; set; }
[DataType(DataType.Currency)]
[Range(2, Int32.MaxValue, ErrorMessage = "Atleast £2.00 or a whole number please")]
public int Amount { get; set; }
[DataType(DataType.Currency)]
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
}
您好我正在尝试获取从“创建”输入的信息以显示在“确认”中,我可以只显示数据库但我想要的是要带到下一页的具体信息,而不是一个完整的数据库。我是 MVC 的新手,所以试图掌握这一点。
我已经在一个新的 ActionResult 上创建了它,但我无法让传递的数据工作。
【问题讨论】:
-
接受的答案链接显示了如何做到这一点。在
Create()POST 方法使用return RedirectToAction("Confirmation", "Charities", new { id = charity.ID });和public ActionResult Confirmation(int id)GET 方法保存后,根据id值再次从数据库中获取模型并将其返回给您Confirmation.cshtml视图。 (你似乎有一个错字 -ConfirmationvsConfiramtion?) -
@StephenMuecke 感谢您的回答,我想我按照您所说的进行了,当我启动“ConfirmationView”时仍然会出现错误 404。
-
404 表示未找到视图。你纠正错别字了吗?
-
@StephenMuecke 是的,问题已更新。我应该创建一个新视图还是 HTTP POST 有问题?
标签: c# asp.net-mvc asp.net-mvc-4