【问题标题】:How to send textbox value and item ID from view to controller razorview如何将文本框值和项目 ID 从视图发送到控制器 razorview
【发布时间】:2017-11-17 10:19:56
【问题描述】:

单击提交按钮后,如何将当前项目 ID 和文本框值发送到控制器。

@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 >

以下是需要参数的控制器。

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

我想了解如何使用帮助标签为上述参数传递项目 ID 和文本框值。我也不确定中间的@using Html.BeginForm()。任何帮助将不胜感激。

【问题讨论】:

  • 您的号码文本框名为amount,因此参数需要为double amount(而不是investmentAmount
  • id是否作为路由值传递取决于您的GET方法的签名,但该参数应该是int Id
  • 我更正了 name=... 的东西。但我不知道如何将 Id 传递给该控制器。 “参数字典包含不可为空类型的参数'propertyId'的空条目”这是我得到的错误。如果你已经回答了这类问题,你能指出我吗?我没有找到我想要的。
  • 您可以为其创建隐藏输入或将其作为路由值添加到您的 BeginForm 中(但如果您的 GET 方法参数正确匹配您的 POST 方法,它将自动添加 - 但您没有显示该代码)
  • 当你的参数是propertyIdinvestmentAmount时,为什么你的路由是AddInvestment/{id}/{amount}(这没有意义)

标签: asp.net asp.net-mvc razor


【解决方案1】:

当您提交表单时(如您的代码所示),数据将作为键值对(名称:值)提交。这是使用输入控件的name 属性完成的。如果您需要跟踪 id,那么您需要将 id 放在它自己的“键值对”中(在这种情况下,是一个单独的输入控件)。像这样的:

        @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" />
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-17
    • 1970-01-01
    • 1970-01-01
    • 2021-04-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多