简单的 MVC 表单帖子和 url 路由问题....我希望

发布于 2024-09-29 04:59:25 字数 2256 浏览 3 评论 0原文

我有一个名为“关联”的视图,其中有一个下拉列表和一个按钮。当用户选择一个选项并按提交时,我希望他们转到 Association/{associationKey}。关联需要处理 get 和 post 问题。

目前,使用下面的代码,当发布表单时,它会转到 Association 并显示正确的记录,但它不会将关联密钥附加到 url 中。

所以我得到:

http://localhost/Association

而不是:

http://localhost/Association/202

如果我手动导航到 http: //localhost/Association/202 一切工作正常,所以 get 和 post 都工作正常....我只想在发布后的 url 中输入密钥!

当然一定有一些超级简单的事情我做错了。相关代码如下。

谢谢!

ASSOCIATIONS 视图:

<% Html.BeginForm("Association", "Staff", FormMethod.Post); %>
<%:Html.DropDownList("associationKey", new SelectList(Model.Associations.ToList(), "AssociationKey", "LegalName"))%>
<input type="submit" value="Edit The Selected Record" />
<% Html.EndForm(); %>

STAFF 控制器:


        [AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)]
        public ActionResult Association(int associationKey)
        {
            return View("Association", new AssociationViewModel(associationKey));
        }

GLOBAL.asax:


        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.MapRoute("Default", "{action}", new { controller = "Staff", action = "Default" });
            routes.MapRoute("Associations", "Associations", new { controller = "Staff", action = "Associations" });
            routes.MapRoute("Association", "Association/{associationKey}", new { controller = "Staff", action = "Association" });
        }

ASSOCIATION 视图模型:


    public class AssociationViewModel
    {

        public Repository db = new Repository();

        public Association Association {get; private set; }

        public List TelephoneTypes { get; private set; }

        public AssociationViewModel(int associationKey)
        {
            Association = db.AssociationById(associationKey);
            TelephoneTypes = db.ListTelephoneTypes().ToList();
        }

    }

I have a view called Associations that has a drop down list and a button. When the user selects an option and presses submit, I want them to go to Association/{associationKey}. Association needs to work on get and post.

Currently, with the code below, when the form is posted, it DOES go to Association and displays the correct record, BUT it does not append the associationKey to the url.

So I am getting:

http://localhost/Association

instead of:

http://localhost/Association/202

If I manually navigate to http://localhost/Association/202 everything works perfectly, so get and post are both working fine....I just want the key in the url after a post!

Surely there must be something super simple I am doing wrong. Relevant code below.

Thanks!

ASSOCIATIONS view:

<% Html.BeginForm("Association", "Staff", FormMethod.Post); %>
<%:Html.DropDownList("associationKey", new SelectList(Model.Associations.ToList(), "AssociationKey", "LegalName"))%>
<input type="submit" value="Edit The Selected Record" />
<% Html.EndForm(); %>

STAFF controller:


        [AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)]
        public ActionResult Association(int associationKey)
        {
            return View("Association", new AssociationViewModel(associationKey));
        }

GLOBAL.asax:


        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.MapRoute("Default", "{action}", new { controller = "Staff", action = "Default" });
            routes.MapRoute("Associations", "Associations", new { controller = "Staff", action = "Associations" });
            routes.MapRoute("Association", "Association/{associationKey}", new { controller = "Staff", action = "Association" });
        }

ASSOCIATION view model:


    public class AssociationViewModel
    {

        public Repository db = new Repository();

        public Association Association {get; private set; }

        public List TelephoneTypes { get; private set; }

        public AssociationViewModel(int associationKey)
        {
            Association = db.AssociationById(associationKey);
            TelephoneTypes = db.ListTelephoneTypes().ToList();
        }

    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

咋地 2024-10-06 04:59:25

我认为您应该将控制器操作分为 Get 操作和 POST 操作,如下所示:

[AcceptVerbs(HttpVerbs.Get)]
public ActionResult Association(int associationKey)
{
    return View("Association", new AssociationViewModel(associationKey));
}

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Association(AssociationViewModel model)
{
    return RedirectToAction("Association", new {associationKey= model.associationKey});
}

MVC 框架会自动将 SelectList 中的选定值绑定到模型(假设您在模型中有一个属性来保存选定值) 。从那里您只需重定向到传入密钥的 GET 方法。

I think you should separate out your controller actions into a Get action and a POST action like so:

[AcceptVerbs(HttpVerbs.Get)]
public ActionResult Association(int associationKey)
{
    return View("Association", new AssociationViewModel(associationKey));
}

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Association(AssociationViewModel model)
{
    return RedirectToAction("Association", new {associationKey= model.associationKey});
}

The MVC framework will automatically bind the selected value from your SelectList to the model (assuming you have a property in the model to hold the selected value). From there you just need to redirect to your GET method passing in the key.

巷子口的你 2024-10-06 04:59:25

它正在执行 post 而不是 GET。这会将值放在表单参数中,而不是放在 url 中。您可能想使用 javascript 拦截表单提交,并使用 location=.. 将其转换为 GET

<script type="text/javascript">
    $(function() { // requires jQuery
        $('form').submit( function() {
            // maybe do some validation to ensure a legal value chosen first?
            location.href = $(this).attr('action') + '/' + $(this).find('select').value();
            return false;  // cancel submit
        });
    });
</script>

<% Html.BeginForm("Association", "Staff", FormMethod.Post); %> 
<%:Html.DropDownList("associationKey", new SelectList(Model.Associations.ToList(), "AssociationKey", "LegalName"))%> 
<input type="submit" value="Edit The Selected Record" /> 
<% Html.EndForm(); %>

It's doing a post instead of a GET. This puts the value in the form parameters not in the url. You might want to intercept the form submit using javascript and turn it into a GET using location=..

<script type="text/javascript">
    $(function() { // requires jQuery
        $('form').submit( function() {
            // maybe do some validation to ensure a legal value chosen first?
            location.href = $(this).attr('action') + '/' + $(this).find('select').value();
            return false;  // cancel submit
        });
    });
</script>

<% Html.BeginForm("Association", "Staff", FormMethod.Post); %> 
<%:Html.DropDownList("associationKey", new SelectList(Model.Associations.ToList(), "AssociationKey", "LegalName"))%> 
<input type="submit" value="Edit The Selected Record" /> 
<% Html.EndForm(); %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文