开发者

Public method from Model not visible on Razor View

开发者 https://www.devze.com 2023-03-04 19:35 出处:网络
I have method on m开发者_C百科y Model: public string GetSubscriptionCode() { return String.Format(Name.ToLower().Replace(\"\", \"-\"));

I have method on m开发者_C百科y Model:

    public string GetSubscriptionCode()
    {
        return String.Format(Name.ToLower().Replace("", "-"));
    }

Than when I want to call that method on the view i am getting an error that there is no such method in my model:

 <div id="@Model.GetSubscriptionCode()"></div>

Any ideas?

Update: Ooops, I mixed up my models, I put my method in to wrong one... Now i would remove this question if i could


Assuming your view is strongly typed to a model:

@model MyViewModel

where MyViewModel contains this public method:

public class MyViewModel
{
    public string Name { get; set; }

    public string GetSubscriptionCode()
    {
        return String.Format(Name.ToLower().Replace("", "-"));
    }
}

you should be able to perfectly fine access it in the view:

<div id="@Model.GetSubscriptionCode()"></div>

Don't worry if Visual Studio underlines it saying that it doesn't exist. Intellisense in views is far from perfect. Simply run your application and it will work as expected.


Maybe it's an extension method so you need to include/import the namespace implementing that extension method.

0

精彩评论

暂无评论...
验证码 换一张
取 消