How to bind view model property with different name

Viewed 22532

Is there a way to make a reflection for a view model property as an element with different name and id values on the html side.

That is the main question of what I want to achieve. So the basic introduction for the question is like:

1- I have a view model (as an example) which created for a filter operation in view side.

public class FilterViewModel
{
    public string FilterParameter { get; set; }
}

2- I have a controller action which is created for GETting form values(here it is filter)

public ActionResult Index(FilterViewModel filter)
{
return View();
}

3- I have a view that a user can filter on some data and sends parameters via querystring over form submit.

@using (Html.BeginForm("Index", "Demo", FormMethod.Get))
{    
    @Html.LabelFor(model => model.FilterParameter)
    @Html.EditorFor(model => model.FilterParameter)
    <input type="submit" value="Do Filter" />
}

4- And what I want to see in rendered view output is

<form action="/Demo" method="get">
    <label for="fp">FilterParameter</label>
    <input id="fp" name="fp" type="text" />
    <input type="submit" value="Do Filter" />
</form>

5- And as a solution I want to modify my view model like this:

public class FilterViewModel
{
    [BindParameter("fp")]
    [BindParameter("filter")] // this one extra alias
    [BindParameter("param")] //this one extra alias
    public string FilterParameter { get; set; }
}

So the basic question is about BindAttribute but the usage of complex type properties. But also if there is a built in way of doing this is much better. Built-in pros:

1- Usage with TextBoxFor, EditorFor, LabelFor and other strongly typed view model helpers can understand and communicate better with each other.

2- Url routing support

3- No framework by desing problems :

In general, we recommend folks don’t write custom model binders because they’re difficult to get right and they’re rarely needed. The issue I’m discussing in this post might be one of those cases where it’s warranted.

Link of quote

And also after some research I found these useful works:

Binding model property with different name

One step upgrade of first link

Here some informative guide

Result: But none of them give me my problems exact solution. I am looking for a strongly typed solution for this problem. Of course if you know any other way to go, please share.


Update

The underlying reasons why I want to do this are basically:

1- Everytime I want to change the html control name then I have to change PropertyName at compile time. (There is a difference Changing a property name between changing a string in code)

2- I want to hide (camouflage) real property names from end users. Most of times View Model property names same as mapped Entity Objects property names. (For developer readability reasons)

3- I don't want to remove the readability for developer. Think about lots of properties with like 2-3 character long and with mo meanings.

4- There are lots of view models written. So changing their names are going to take more time than this solution.

5- This is going to be better solution (in my POV) than others which are described in other questions until now.

2 Answers
Related