0

How do I send the current item ID and textbox value to the controller after clicking submit button.

@model propertyMgmt.Models.Property
  <div class="container" id="tourpackages-carousel">
    <div class="panel panel-default" style="width:inherit">
      <div class="panel-heading">
        <h4 style="font-family:Tahoma, Geneva, sans-serif;margin-left:-5px;">
        <strong>@Model.PropertyDescription</strong></h4>
      </div>
      <div class="panel-body">
        <div class="row">
          <div class="col-md-6">
            <div>
              <img src="~/Image/@Model.PropertyImage" alt="" style="width:100%;height:300px">
            </div>
          </div>
          <div class="col-md-6" style="margin-left:-8px;margin-top:-20px;">
            <div style="background-color:#d9dadd">
              <h3 style="margin-left:16px;">
              <u>Property Details</u></h3>
            <dl class="dl-horizontal">
              <dt>Total Investment:</dt>
              <dd>@Model.InvestmentAmount</dd>
              <dt>Remaining Investment:</dt>
              <dd>@Model.RemainingInvestmentAmount</dd>
              <dt>Property Discription:</dt>
              <dd>@Model.PropertyDescription</dd>
              <dt>Property Cost:</dt>
              <dd>@Model.PropertyCost</dd>
              <dt>Country:</dt>
              <dd>@Model.Country</dd>
              <dt>City:</dt>
              <dd>@Model.City</dd>
            </dl>

            @using (Html.BeginForm("AddInvestment","Home",FormMethod.Post))
            {
            <input type="number" id="investmentAmount" name="investmentAmount" /><br />
            <input type="submit" value="Submit" />
            }
          </ div >
        </ div >
      </ div >
    </ div >
</ div >
</ div >

The following is the controller with the parameters needed.

[HttpPost]
[Route("AddInvestment/{id}/{amount}")]
public ActionResult AddInvestment(int propertyId, double investmentAmount)
{
    Investment investment = new Investment();
    investment.PropertyId = propertyId;
    investment.InvestorId = Convert.ToInt32(Session["UserId"]);
    investment.InvestmentAmount = investmentAmount;
    _investmentQueryProcessor.AddInvestment(investment);
    RemainingInvestment(propertyId, investmentAmount);
    RemainingInvestorBalance(investment.InvestorId, investmentAmount);
    return View();
}

I want to understand how I can pass the item Id and textbox value for above parameters using helper tags. I am also not sure about the @using Html.BeginForm() in the middle. Any help would be appreciated.

KevinLamb
  • 634
  • 8
  • 18
  • Your number textbox is named `amount` so the parameter needs to be `double amount` (not `investmentAmount`) –  Nov 17 '17 at 10:22
  • And whether the `id` is passed as a route value depends on the signature of your GET method, but that parameter should be `int Id` –  Nov 17 '17 at 10:24
  • I corrected that name=... thing.But I have no idea how to pass the Id to that controller. "The parameters dictionary contains a null entry for parameter 'propertyId' of non-nullable type " This is the error I get.If you have already answered this type of question can you point me to it.I did not find what i wanted. – Nitesh Shrestha Nov 17 '17 at 10:29
  • You can create a hidden input for it or add it as a route value in your `BeginForm` (but it would be automatically added if your GET method parameters are correct to match your POST method - but you have not shown that code) –  Nov 17 '17 at 10:34
  • 1
    And why is your route `AddInvestment/{id}/{amount}` when your parameters are `propertyId` and `investmentAmount` (that makes no sense) –  Nov 17 '17 at 10:36
  • There is no httpget :| in my controller. – Nitesh Shrestha Nov 17 '17 at 10:36
  • what should they be? :| – Nitesh Shrestha Nov 17 '17 at 10:36
  • Isn't that how you pass parameters.:| – Nitesh Shrestha Nov 17 '17 at 10:38
  • Of course there is a GET method - the one you used to generate the view :) –  Nov 17 '17 at 10:39
  • public ActionResult PropertyDetails(int? id) { var data = _propertyQueryProcessor.GetById(id); return View(data); } This is what my PropertyDetails.cshtml page uses.It was not auto generated :|. The id here is int? id and the post method has only int propertyId.Is that the problem? – Nitesh Shrestha Nov 17 '17 at 10:41
  • And since you have `AddInvestment/{id}/{amount}` then the parameters should be `int id, double amount` (and change the name of the control back to `name="amount"`). But of course you should be using a view model with a property `double Amount` and strongly bind to it using `@Html.EditorFor(m => m.Amount)` and `@Html.HiddenFor(m => m.Id)` –  Nov 17 '17 at 10:43
  • So what you are saying is there MUST be a property in MODEL that should be bound with the textbox I am trying to use. – Nitesh Shrestha Nov 17 '17 at 11:04
  • No is not mandatory - you can always use `` and `` and not have strong typed binding. And not have any validation on either the client or the server. And spend wasted hours with errors because of it. Its up to you :) –  Nov 17 '17 at 11:09
  • Ok.Sassy!! :D Here's the thing.This textbox has no Model Property to bind to.This amount is used to deduct the totalAmount that is already present in the Model.I did tell my teammates that we needed a model property to hold this value.They said it was not necessary so i wanted to know.How do i proceed without any model property? :| – Nitesh Shrestha Nov 17 '17 at 11:15
  • Your editong data so **ALWAYS** use a view model, and that view model will contain a property named `Amount`. And it will include validation attributes (for example `[Required]` or `[Range]` etc) so you can also validate the data. - [What is ViewModel in MVC?](https://stackoverflow.com/questions/11064316/what-is-viewmodel-in-mvc). –  Nov 17 '17 at 11:18
  • And you will probably want a conditional validation attribute to ensure its not more that the `totalAmount`. And your POST method has `return View()` - do you really have a view named `AddInvestment.cshtml`? or are you expecting to return the current view - in which case it would need to be `return View("PropertyDetails", xxx)` were `xxx` is an instance of your `Property` model –  Nov 17 '17 at 11:22
  • No,there is no AddInvestment.cshtml page. xxx instance meaning?? Also,when using loose binding the parameters do get sent to the controller but i dbupdate exception.Anyway you have helped me quite alot in the pas few days.Thank You so much. I still have lots more to ask. – Nitesh Shrestha Nov 17 '17 at 11:31
  • `return View()` means return a view with the same name as the method (i.e. return a view named `AddInvestment.cshtml`) and do not send it a model. If you want to return to the `PropertyDetails.cshtml` view, then your need `return View("PropertyDetails");` But because you have not passed a model to it then `
    @Model.InvestmentAmount
    ` would throw an exception (you cannot access a property of a `null`. Therefore you need to initialize your model again - say `var model = new Property();` and set its properties and then pass it to the view using `return View("PropertyDetails", model);`
    –  Nov 17 '17 at 11:37
  • But why would you want to return the same view anyway - do you want then to edit the amount again? You should be returning to a different view (perhaps a 'Details` view of the `Property` that just shows the updated model and maybe some kind of confirmation message (and without the form) –  Nov 17 '17 at 12:02
  • Thank You Stephen!! It is solved now!! .Thank You So much!! :D – Nitesh Shrestha Nov 18 '17 at 04:55

1 Answers1

0

When you submit the form (as your code shows), the data gets submitted as key-value pairs (name:value). This is done using the name attribute of the input controls. If you need to track the id, then you will need to put the id in its own "key-value pair" (in this case, a separate input control). Something like this:

        @using (Html.BeginForm("AddInvestment","Home",FormMethod.Post))
        {
            <input type="number" id="investmentAmount" name="investmentAmount" /><br />
            <input type="hidden" id="propertyId" name="propertyId" value="@Model.PropertyId" /><br />
            <input type="submit" value="Submit" />
        }
Matt Spinks
  • 6,380
  • 3
  • 28
  • 47