ASP.Net MVC: Calling a method from a view

前端 未结 8 1159
轻奢々
轻奢々 2020-11-29 02:52

In my MVC app the controller gets the data (model) from an external API (so there is no model class being used) and passes that to the view. The data (model) has a container

相关标签:
8条回答
  • 2020-11-29 03:04

    You can implement a static formatting method or an HTML helper, then use this syntaxe :

    @using class_of_method_namespace
    ...
    // HTML page here
    @className.MethodName()
    

    or in case of HTML Helper

    @Html.MehtodName()
    
    0 讨论(0)
  • 2020-11-29 03:06

    Building on Amine's answer, create a helper like:

    public static class HtmlHelperExtensions
    {
        public static MvcHtmlString CurrencyFormat(this HtmlHelper helper, string value)
        {
            var result = string.Format("{0:C2}", value);
            return new MvcHtmlString(result);
        }
    }
    

    in your view: use @Html.CurrencyFormat(model.value)

    If you are doing simple formating like Standard Numeric Formats, then simple use string.Format() in your view like in the helper example above:

    @string.Format("{0:C2}", model.value)
    
    0 讨论(0)
  • 2020-11-29 03:14

    why You don't use Ajax to

    its simple and does not require page refresh and has success and error callbacks

    take look at my samlpe

    <a id="ResendVerificationCode" >@Resource_en.ResendVerificationCode</a>
    

    and in JQuery

     $("#ResendVerificationCode").on("click", function() {
                    getUserbyPhoneIfNotRegisterd($("#phone").val());
     });
    

    and this is my ajax which call my controller and my controller and return object from database

    function getUserbyPhoneIfNotRegisterd(userphone) {
    
                  $.ajax({
                        type: "GET",
                        dataType: "Json",
                        url: '@Url.Action("GetUserByPhone", "User")' + '?phone=' + userphone,
                        async: false,
                        success: function(data) {
                            if (data == null || data.data == null) {
                                ErrorMessage("", "@Resource_en.YourPhoneDoesNotExistInOurDatabase");
                            } else {
                                user = data[Object.keys(data)[0]];
                                    AddVereCode(user.ID);// anather Ajax call 
                                    SuccessMessage("Done", "@Resource_en.VerificationCodeSentSuccessfully", "Done");
                            }
                        },
                        error: function() {
                            ErrorMessage("", '@Resource_en.ErrorOccourd');
                        }
                    });
                }
    
    0 讨论(0)
  • 2020-11-29 03:17

    This is how you call an instance method on the Controller:

    @{
      ((HomeController)this.ViewContext.Controller).Method1();
    }
    

    This is how you call a static method in any class

    @{
        SomeClass.Method();
    }
    

    This will work assuming the method is public and visible to the view.

    0 讨论(0)
  • 2020-11-29 03:17

    I tried lashrah's answer and it worked after changing syntax a little bit. this is what worked for me:

    @(
      ((HomeController)this.ViewContext.Controller).Method1();
    )
    
    0 讨论(0)
  • 2020-11-29 03:23

    You should not call a controller from the view.

    Add a property to your view model, set it in the controller, and use it in the view.

    Here is an example:

    MyViewModel.cs:

    public class MyViewModel
    {   ...
        public bool ShowAdmin { get; set; }
    }
    

    MyController.cs:

    public ViewResult GetAdminMenu()
        {
            MyViewModelmodel = new MyViewModel();            
    
            model.ShowAdmin = userHasPermission("Admin"); 
    
            return View(model);
        }
    

    MyView.cshtml:

    @model MyProj.ViewModels.MyViewModel
    
    
    @if (@Model.ShowAdmin)
    {
       <!-- admin links here-->
    }
    

    ..\Views\Shared\ _Layout.cshtml:

        @using MyProj.ViewModels.Common;
    ....
        <div>    
            @Html.Action("GetAdminMenu", "Layout")
        </div>
    
    0 讨论(0)
提交回复
热议问题