MVC6输入asp-for not change model属性值

时间:2015-10-20 18:26:26

标签: asp.net-core-mvc

我尝试更新模型属性值,但在httpost中没有反映输入的新值,显示原始值。 谢谢你的帮助。

<div class="row row-padding">
<section class="form-field">
    <input asp-for="bidDataProcess.MaxBidPosted" type="number" class="form-control" />
    <form asp-controller="Item" asp-action="PlaceBid" asp-route-ItemId="@Model.itemDescription.ItemId"  asp-route-MaxBid="@Model.bidDataProcess.MaxBidPosted" method="post" class="clearfix">
        <input id="PlaceBid" type="submit" style="background-color:blue;color:white; font-weight:bold;width:50%" class="form-control text-center" value="Place Bid" />
   </form>
</section>

<小时/>

[HttpPost]
public ActionResult PlaceBid(int ItemId, decimal MaxBid)
{
// Maxbid resturns the original value  not the new value entered.
decimal maxBid = MaxBid;
return View();
}

1 个答案:

答案 0 :(得分:0)

  • 您无需将文本框表单元素复制为输入和路由值。
  • 您的MaxBidPosted输入需要位于表单标记内,否则控制器中的MaxBid将为零
  • 您的输入名称(MaxBidPosted)需要与您的控制器参数(MaxBid)匹配,否则控制器中的MaxBid将为零

CSHTML:

    <div class="row row-padding">
        <section class="form-field">
            <form asp-controller="Item" asp-action="PlaceBid" asp-route-ItemId="@Model.ItemId" method="post" class="clearfix">
                <input asp-for="MaxBidPosted" type="number" class="form-control" />
                <input id="PlaceBid" type="submit" style="background-color:blue;color:white; font-weight:bold;width:50%" class="form-control text-center" value="Place Bid" />
            </form>
        </section>
    </div>

控制器:

    [HttpPost]
    public ActionResult Index(int ItemId, decimal MaxBidPosted)
    {
        decimal maxBid = MaxBidPosted;
        return View();
    }