【发布时间】: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 方法,它将自动添加 - 但您没有显示该代码) -
当你的参数是
propertyId和investmentAmount时,为什么你的路由是AddInvestment/{id}/{amount}(这没有意义)
标签: asp.net asp.net-mvc razor