如何绑定不同的名称视图模型属性 [英] How to bind view model property with different name

查看:202
本文介绍了如何绑定不同的名称视图模型属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法,使一个视图模型属性作为与HTML端不同的名称和ID值的元素的体现。

这是我所要实现的主要问题。因此,对于这个问题的基本介绍就像是:

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

1-我有在观看侧的过滤器操作创建的图模型(作为一个例子)。

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 - 我有一个是获取表单值(这里是过滤器)创建了一个控制器动作

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我有一个观点,即用户可以在一些数据进行过滤,并通过查询字符串重于形式发送参数提交。

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和我想要的渲染视图输出看到的是

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年和作为一个解决方案,我想修改我的视图模型是这样的:

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; }
}

所以,基本的问题是关于BindAttribute但复杂类型属性的使用。而且,如果有一个内置在这样做的方式是要好得多。
内置的优点:

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用法与TextBoxFor,EditorFor,LabelFor等强类型的视图模型助手可以理解和相互沟通更好。

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

2 - URL路由支持

2- Url routing support

3由德兴问题无框架:

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.

报价

和也经过一番研究,我发现这些有用的作品:

And also after some research I found these useful works:

<一个href=\"http://stackoverflow.com/questions/4316301/asp-net-mvc-2-bind-a-models-property-to-a-different-named-value\">Binding用不同的名字模型属性。

<一个href=\"http://ole.michelsen.dk/blog/bind-a-model-property-to-a-different-named-query-string-field/\">One第一个链接步骤升级

<一个href=\"http://stackoverflow.com/questions/8507699/how-to-bind-url-parameters-to-model-properties-with-different-names\">Here一些信息指南

结果:但是没有人给我确切的问题的解决方案。我找了这个问题的强类型的解决方案。当然,如果你知道任何别的路可走,请分享。

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.

更新

的根本原因,我想这样做基本上是:

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

1 - 每次我想改变HTML控件的名字,然后我在编译时改变属性名。 (有改变code字符串之间更改属性名的差)

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 - 我想隐藏(迷彩)来自最终用户的真实属性名称。大部分时间看一样映射实体对象的属性名称型号属性名称。 (对于开发商可读性的原因)

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,我不希望删除的可读性开发商。想想很多属性像2-3个字符长的和莫含义。

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-有很多视图模型编写的。因此,改变他们的名字将采取更多的时间比这个解决方案。

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

5这将是更好的解决方案(在我的POV)比别人这在其他问题中描述到现在。

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

推荐答案

简短的回答是否定的,长的答案仍然没有。没有内置的辅助,属性模型绑定,无论是它(没有开盒即用)。

The short answer is NO and long answer still NO. There is no built-in helper, attribute, model binder, whatever is it (Nothing out of box).

但我的答案(我删除了它)是,我昨天来实现一个可怕的解决方案之前做过英寸我打算把它放在GitHub上了谁仍然希望看到(也许有人解决问题),(我不认为它也!)

But what I did in before answer (I deleted it) was an awful solution that I realized yesterday. I am going to put it in github for who still wants to see (maybe it solves somebody problem) (I don't suggest it also!)

现在我再次搜查了,我找不到任何帮助。如果你正在使用类似AutoMapper或ValueInjecter类似工具,为您的ViewModel对象映射到业务对象,如果你想进行模糊处理该视图模型的参数也可能你是在一些麻烦。当然,你可以这样做,但强类型HTML辅助方法不会帮助你很多。我甚至不是在谈论如果其他开发商采取分公司和超过普通视图模型的工作。

Now I searched it for again and I couldn't find anything helpful. If you are using something like AutoMapper or ValueInjecter like tool for mapping your ViewModel objects to Business objects and if you want to obfuscate that View Model parameters also, probably you are in some trouble. Of course you can do it but strongly typed html helpers are not going to help you alot. I even not talking about the if other developers taking branch and working over common view models.

幸运的是我的项目(​​4人做这个工作的,其用于商业用途)没有那么大,现在,所以我决定改变视图模型属性名称! (它仍然是很多工作要做。数以百计的视图模型来混淆他们的财产!)谢谢Asp.Net MVC!

Luckily my project (4 people working on it, and its commercial use for) not that big for now, so I decided to change View Model property names! (It is still lot work to do. Hundreds of view models to obfuscate their properties!!!) Thank you Asp.Net MVC !

有我在其中的问题给了链接的一些方法。但也如果你仍然想使用BindAlias​​属性,我只能建议你使用下面的扩展方法。至少你没有写,你在BindAlias​​属性写相同的别名字符串。

There some ways in the links which I gave in question. But also if you still want to use the BindAlias attribute, I can only suggest you to use the following extension methods. At least you dont have to write same alias string which you write in BindAlias attribute.

下面是:

public static string AliasNameFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper,
    Expression<Func<TModel, TProperty>> expression)
{
    var memberExpression = ExpressionHelpers.GetMemberExpression(expression);
    if (memberExpression == null)
        throw new InvalidOperationException("Expression must be a member expression");
    var aliasAttr = memberExpression.Member.GetAttribute<BindAliasAttribute>();
    if (aliasAttr != null)
    {
        return MvcHtmlString.Create(aliasAttr.Alias).ToHtmlString();
    }
    return htmlHelper.NameFor(expression).ToHtmlString();
}

public static string AliasIdFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper,
    Expression<Func<TModel, TProperty>> expression)
{
    var memberExpression = ExpressionHelpers.GetMemberExpression(expression);
    if (memberExpression == null)
        throw new InvalidOperationException("Expression must be a member expression");
    var aliasAttr = memberExpression.Member.GetAttribute<BindAliasAttribute>();
    if (aliasAttr != null)
    {
        return MvcHtmlString.Create(TagBuilder.CreateSanitizedId(aliasAttr.Alias)).ToHtmlString();
    }
    return htmlHelper.IdFor(expression).ToHtmlString();
}



public static T GetAttribute<T>(this ICustomAttributeProvider provider)
    where T : Attribute
{
    var attributes = provider.GetCustomAttributes(typeof(T), true);
    return attributes.Length > 0 ? attributes[0] as T : null;
}

public static MemberExpression GetMemberExpression<TModel, TProperty>(Expression<Func<TModel, TProperty>> expression)
{
    MemberExpression memberExpression;
    if (expression.Body is UnaryExpression)
    {
        var unaryExpression = (UnaryExpression)expression.Body;
        memberExpression = (MemberExpression)unaryExpression.Operand;
    }
    else
    {
        memberExpression = (MemberExpression)expression.Body;
    }
    return memberExpression;
}

当你想使用它:

[ModelBinder(typeof(AliasModelBinder))]
public class FilterViewModel
{
    [BindAlias("someText")]
    public string FilterParameter { get; set; }
}

在HTML:

@* at least you dont write "someText" here again *@
@Html.Editor(Html.AliasNameFor(model => model.FilterParameter))
@Html.ValidationMessage(Html.AliasNameFor(model => model.FilterParameter))

所以我在这里离开这个答案是这样的。这甚至不是一个答案(而且没有答案MVC 5),但谁在谷歌搜索同样的问题,可能会发现有用的这方面的经验。

So I am leaving this answer here like this. This is even not an answer (and there is no answer for MVC 5) but who searching in google for same problem might find useful this experience.

这里是github上回购: https://github.com/yusufuzun /所以视图模型绑定-20869735

And here is the github repo: https://github.com/yusufuzun/so-view-model-bind-20869735

这篇关于如何绑定不同的名称视图模型属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆