0

I'm involved in ASP.NET MVC project which requires me to complete on it. Since I'm totally new to MVC approach I found something which I couldn't understand I tried to search for it but nothing been found I even don't know how to ask the question properly so excuse me.

I want to bind a model's Property to a resource string so when using @HTML.LabelFor(model => model.Property) it got rendered with the right resource string. I know that it can be binded using data anotations by adding this line in the model:

[Display(Name = "ResourceStringName", ResourceType = typeof(MyResource))]
string CompanyName {get; set;};

This approach isn't good because when I want to update my model from database it will be overwritten. But in the project that I'm working on the properties got binded to the right resource string without adding data annotations in the model but I can't figure out how to do this for a newly added property from database. Do anyone know how to bind a resource string to model property without adding data annotations ?

EDIT

This is a piece of View:

<div class="form-group clearfix">
                    <label class="col-sm-4 col-sm-offset-1 control-label">
                        @Html.LabelFor(model => model.RejectedEmailTemplate)
                        <span class="template">@Infas.OnlineApplications.Resources.MyResource.RejectedEmailTemplate</span>
                    </label>
                    <div class="col-sm-6">
                        @Html.TextAreaFor(model => model.RejectedEmailTemplate, new { disabled = "disabled", @class = "emailTemplate form-control", rows = 12 })
                        @Html.ValidationMessageFor(model => model.RejectedEmailTemplate)
                    </div>
                </div>

                <div class="form-group clearfix">
                    <label class="col-sm-4 col-sm-offset-1 control-label">
                        @Html.LabelFor(model => model.ReturnApplicationEmailTemplate)
                        <span class="template">@Infas.OnlineApplications.Resources.MyResource.ReturnApplicationEmailTemplate</span>
                    </label>
                    <div class="col-sm-6">
                        @Html.TextAreaFor(model => model.ReturnApplicationEmailTemplate, new { disabled = "disabled", @class = "emailTemplate form-control", rows = 12 })
                        @Html.ValidationMessageFor(model => model.ReturnApplicationEmailTemplate)
                    </div>
                </div>
Ibrahim Amer
  • 1,147
  • 4
  • 19
  • 46
  • You must show the view, or we can't help you! Probabily a custom Helper or direct access to the resource string. – Fals Aug 04 '15 at 16:19
  • @Fals sorry for that, here you go – Ibrahim Amer Aug 04 '15 at 16:23
  • 2
    Using proper view models instead of binding your DB models directly to your views would solve this problem (and a whole host of other problems you are going to face if you keep doing that). – Ant P Aug 04 '15 at 16:28

1 Answers1

0

Are you generating your Model using Entity Framework ?

Because if it's the case there is a way to add data annotations without manually editing generated code, it's called a metadata class, here is the concrete example: Add data annotations to a class generated by entity framework

If no, I would suggest to use a data transfer object class with the annotations you need and a mapper for the properties of your model. Source

Community
  • 1
  • 1
Giu
  • 1,832
  • 2
  • 16
  • 31