在部分资源中找不到BeginForm

本文关键字:找不到 BeginForm 资源 | 更新日期: 2023-09-27 18:12:20

我有一个路由问题,我不能弄清楚:

部分方法:

public ActionResult UserProjMetric(int id, DateTimeOffset? startDate, DateTimeOffset? endDate){
   // some logic code here
   return PartialView("_UserProjMetric", model);
}

主要观点:

<div id="dailyMetric">
            @{
                Html.RenderAction("UserProjMetric", "Users", new { id = Model.Id });
            }
        </div>

以上都很有效。我的问题是在部分我正在做一个HTML。BeginForm这不起作用。

<div class="dateContainer">
                        @using (Html.BeginForm("UserProjMetric", "Users", new { id = 23 }))
                        {
                            <span class=""><input type="text" class="startDate dates" placeholder="Start Date" id="from" name="from"></span>
                            <span class=""><input type="text" class="endDate dates" placeholder="End Date" id="to" name="to"></span>
                            <input class="btn btn-small" type="submit" value="Submit" />
                        }

                    </div>

我错过了什么?

在部分资源中找不到BeginForm

您最好定义一个像这样的模型类:

public class UserProjMetricModel
{
   public int id {get; set;} 
   public DateTimeOffset? startDate {get; set;} 
   public DateTimeOffset? endDate {get; set;}
}

,然后添加动作方法签名,如

public ActionResult UserProjMetric(UserProjMetricModel userProjMetric)

路由器应该正确地识别要路由到的正确端点。

另外,不要忘记在视图中删除id路由值new { id = 23 },而是将其作为隐藏字段插入

@using (Html.BeginForm("UserProjMetric", "Users", FormMethod.Post))
{
   <input type="hidden" name="id" value="23"/>
   <span class=""><input type="text" class="startDate dates" placeholder="Start Date" id="from" name="from"></span>
   <span class=""><input type="text" class="endDate dates" placeholder="End Date" id="to" name="to"></span>
   <input class="btn btn-small" type="submit" value="Submit" />
 }

我建议您从表单元素中删除id=23,并将其作为表单中的隐藏元素。我想其他代码都能正常工作。

<input type="hidden" id="id" name="id" value="23" />