Я пытаюсь передать Детали пользователь вошел в «Create» взгляд на другой вид «Подтверждение» Код ниже:Как передать конкретные данные из одного вида к другому
Создать Действие Результат
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>
AdditionalI NFO ActionResult
[HttpGet]
public ActionResult Additionalinfo(int id)
{
return View("Additionalinfo", new { id });
}
AdditionalInfo Посмотреть
@{
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, который работает, но я не могу заставить данные передачи работать.
Возможного дубликат [.NET MVC?: Вызов RedirectToAction прохождения модели] (http://stackoverflow.com/questions/2324044/net-mvc-calling -redirecttoaction-through-a-model) –
Принятый ответ, ссылка показывает, как это сделать. После сохранения в методе 'Create()' POST используйте 'return RedirectToAction (« Подтверждение »,« Благотворительность », новый {id = charity.ID});' и в вашем 'public ActionResult Confirmation (int id)' GET method , снова получите модель из базы данных на основе значения 'id' и верните ее в виде' Confirmation.cshtml'. (и у вас, похоже, есть опечатка - 'Подтверждение' против' Confiramtion'?) –
@StephenMuecke Спасибо за ответ, я думаю, что я следовал тому, что вы сказали, ошибка 404 все еще возникает, когда я запускаю «ConfirmationView». – John