-1

Hi can you help in sending a static value of the field in another view field..

so when the user clicked the button it will directly go to the page

my view page of the static value

@using (Html.BeginForm())
    {

<div class="form-horizontal">
    <h4>Customer</h4>
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })

    <div class="form-group">
        @Html.LabelFor(model => model.Price, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Price, new { htmlAttributes = new { @Value = "5", @readonly = "readonly", @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Price, "", new { @class = "text-danger" })
        </div>
    </div>


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

to this view

<div class="form-horizontal">
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <div class="form-group">

            <div class="form-group">
                @Html.LabelFor(model => model.Payment.Amount, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.Payment.Amount, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.Payment.Amount, "", new { @class = "text-danger" })
                </div>
            </div>

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

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

Controller 1

        public ActionResult Pay(Payment apsp)
    {

        decimal amount = apsp.Amount;

        Payment pay = new Payment
        {

            Amount = amount

        };


        return RedirectToAction("Create");
    }

Model

public decimal Amount{ get; set; }

2 Answers2

0

Currently your Pay action method returns a RedirectResult, which is basically a 302 response which tells the browser to make a new HTTP GET call to the Create action method url. If you want to pass some data, you should return the view instead of this redirect result and pass the view model to the View method call.

So replace

 return RedirectToAction("Create");

with

return View("Create",pay);

Also there is no reason to create a new object if you are only reading one property and assigning it to same object type.

public ActionResult Pay(Payment apsp)
{
   return View("Create",apsp);
}

But from your question, It looks like your first view and second view are strongly typed to different view models. For the above code to work, Both should be strongly typed to same view model (as you are passing the same object of Payment)

Note. It is possible to pass (minimal) data via ReidrectToAction method call. Read the below post for more information about different ways to achieve that.

How do I include a model with a RedirectToAction?

Community
  • 1
  • 1
Shyju
  • 214,206
  • 104
  • 411
  • 497
0

I will assume that you know how the HttpPost and HttpGet work.

You can pass your viewmodel via TempData like this:

[HttpGet]
public ActionResult Pay()
{
    return View(new Payment());
}

[HttpPost]
public ActionResult Pay(Payment payment)
{
    TempData["Payment"] = payment;

    return RedirectToAction("Create");
}

[HttpGet]
public ActionResult Create()
{
    if (TempData["Payment"] == null) throw new Exception("Error");

    var payment = TempData["Payment"] as Payment;

    return View(payment);
}
Community
  • 1
  • 1
jomsk1e
  • 3,585
  • 7
  • 34
  • 59