2

I am trying to transfer details the user has entered in the "Create" view to another view "Confirmation" Code is below:

Create Action Result

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);
    }

Create View

    @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 });
    }

AdditionalInfo View

@{
    ViewBag.Title = "Additionalinfo";
}

<h2>Additionalinfo</h2>

Model

  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
}

Hi I am trying to get the information Inputted from the "Create" to be displayed in the "Confirmation", I can just display the database but what I want is the specific information to be carried onto the next page, rather than a whole database. I am new to MVC so trying to get the hang of this.

I have created it on a new ActionResult which works but I can't get the passing data to work.

mnwsmit
  • 1,198
  • 2
  • 15
  • 31
John
  • 117
  • 1
  • 2
  • 13
  • 5
    Possible duplicate of [.NET MVC : Calling RedirectToAction passing a model?](http://stackoverflow.com/questions/2324044/net-mvc-calling-redirecttoaction-passing-a-model) – austin wernli Mar 08 '16 at 18:21
  • The accepted answer the link shows exactly how to do this. After you save in the `Create()` POST method use `return RedirectToAction("Confirmation", "Charities", new { id = charity.ID });` and in your `public ActionResult Confirmation(int id)` GET method, get the model again from the database based on the `id` value and return it to you `Confirmation.cshtml` view. (and you seem to have a typo - `Confirmation` vs `Confiramtion`?) –  Mar 09 '16 at 00:56
  • @StephenMuecke Thanks for the answer, I think I followed what you said, the Error 404 still occurs when I launch the "ConfirmationView". – John Mar 09 '16 at 20:33
  • 404 means the view was not found. Did you correct the typos? –  Mar 09 '16 at 20:40
  • @StephenMuecke yes, the questions updated. Should I just create a new view or is there something wrong with the HTTP POST? – John Mar 09 '16 at 20:44
  • All you have shown is the POST method for `Confirmation`. Do you have a GET method as I noted in my first comment? –  Mar 09 '16 at 20:48
  • @StephenMuecke I am unsure on how to set the get method, I have done some research and think I have put it in next to the POST method, I have created a new ActionResult(AdditionalInfo) because the other one wasnt working. Should I have one or two action results? please check the updated code. – John Mar 09 '16 at 21:29
  • In you `Create()` POST method, you now have `return RedirectToAction("Confirmation", "Charities" , new { id = charity.ID })` so you need a `[HttpGet] public ActionResult Confirmation(int id)` method (refer my first comment) –  Mar 09 '16 at 21:31

1 Answers1

1

You may use session for that:

var products=Db.GetProducts();

//Store the products to a session
Session["products"]=products;

//To get what you have stored to a session
var products=Session["products"] as List<Product>;
Ovais Khatri
  • 3,201
  • 16
  • 14