MVC Controller parameter for a form element with a dot in it?

前端 未结 3 1993
孤城傲影
孤城傲影 2021-02-19 18:45

If you\'re using the Html.TextBoxFor() type methods, you may well end up with Form controls that have dots in their names, like this:

相关标签:
3条回答
  • 2021-02-19 19:10

    I have found another way, a kind of hack because I believe this is misuse of BindAttribute, to associate firstName parameter with Contact.FirstName input element:

    [HttpPost]
    public ActionResult FooAction([Bind(Prefix="Contact.FirstName")]string firstName)
    

    This for sure works with ASP.NET MVC 1.

    0 讨论(0)
  • 2021-02-19 19:11

    Depending on the other form controls, you should be able to have the MVC default model binder construct a Contact object for you. Then the signature of your action method would be:

    [HttpPost]
    public ActionResult FooAction(Contact contact)
    

    Then the Contact.FirstName (and any other fileds) will be bound correctly

    0 讨论(0)
  • 2021-02-19 19:12

    As Clicktricity suggests in comments you may use

    [HttpPost]
    public ActionResult FooAction(FormCollection form)
    {
        firstName = form["Contact.FirstName"];
    }
    
    0 讨论(0)
提交回复
热议问题