Html.BeginForm with html attributes asp.net mvc4

前端 未结 4 413
难免孤独
难免孤独 2021-01-03 21:21

I have Edit Action with Html.BeginForm. How can I add HTML attributes?

I know only one way:

@using (Html.BeginForm(\"Edit\", \"Clients\"         


        
相关标签:
4条回答
  • 2021-01-03 22:10

    Calling via an ActionLink from ControllerA

    @using (Html.BeginForm("Create",
        "StudentPChoice",
        new { StudentPChoiceId = Model.StudentPChoiceId },
        FormMethod.Post))
    {
    
    }
    

    OR

    @using (Html.BeginForm("Create",
        "ControllerB",
        new { ControllerBId = Model.ControllerAId },
        FormMethod.Post))
    {
    
    }
    
    0 讨论(0)
  • 2021-01-03 22:12

    The override you need is:

    @using( Html.BeginForm("Edit", "Clients", new { Id=Model.Id},
                           FormMethod.Post, new { @class = "example" } ) )
    {
    }
    
    • Route values like "id" are passed as the third parameter.
    • HTML attributes like "class" are passed as the fifth parameter.

    See MSDN docs.

    0 讨论(0)
  • 2021-01-03 22:17

    The Action and Controller parameters can also be null to use the default action:

    Html.BeginForm( null, null, FormMethod.Post, new { id=”formname”, @class="formclass" })
    
    0 讨论(0)
  • 2021-01-03 22:18

    If this might be helpful for some people, this works for me:

    @using (Html.BeginForm("RefreshData", "Home", FormMethod.Post, 
            new { Id = "timerangeId", @name = "timerange" }))
        {
            // form elements and input
        }
    

    In Javascript:

    document.getElementById("timerangeId").submit();
    
    0 讨论(0)
提交回复
热议问题