简体   繁体   English

如何在MVC 4剃须刀中获取视图名称

[英]How can get view name in mvc 4 razor

I'm want to read a active View name and fill it to ViewName public Class 我想读取一个活动的View名称并将其填充到ViewName public Class中

I make a Model Class where I'm fetching textBox data with 我制作了一个模型类,在其中获取textBox数据

public string ViewName { get; set; }
public string Name { get; set; }       
public string Email { get; set; }
public string Comment { get; set; }

in layout I have written a code in form tag it was send text box value in rest of public string class's 在布局中,我已经在表单标签中编写了代码,这是在其余公共字符串类的发送文本框值

<form method="post" action="/Home/SendMail" id="contactfrm" role="form">
                    <div class="col-sm-4">
                        <div class="form-group">
                            <label for="name">Name</label>
                            @Html.TextBox("Name", null, new { id = "name", @class = "form-control", placeholder = "Enter name", title = "Please enter your name (at least 2 characters)" })
                        </div>
                        <div class="form-group">
                            <label for="email">Email</label>
                            @Html.TextBox("Email", null, new { id = "email", @class = "form-control", placeholder = "Enter name", title = "Please enter a valid email address)" })
                        </div>
                    </div>
                    <div class="col-sm-4">
                        <div class="form-group">
                            <label for="comments">Comments</label>
                            @Html.TextArea("Comment", null, new { id = "comments", @class = "form-control", TextMode = "MultiLine", Columns = "3", Rows = "5", placeholder = "Enter your message…", title = "Please enter your message (at least 10 characters)" })
                        </div>
                        <button name="submit" type="submit" class="btn btn-lg btn-primary" id="submit">Send Your Message</button>
                        <div class="result"></div>
                    </div>
                </form>

what i want to do now I want to read active view name also for fill the ViewName public class which for post back it again with same view are active in layout. 我现在想做的是,我还想读取活动视图名称,以填充ViewName公共类,以便使用相同的视图再次将其发回布局中。 Have you guys any idea how can I read the viewName and fill it in Public Class? 你们知道我如何阅读viewName并将其填写到Public Class中吗? please let me know where in can add this in Form tag. 请让我知道在哪里可以在Form标签中添加此内容。

Thank You in advance 先感谢您

There are 2 possible ways to get View hame on Razor View. 有两种可能的方法可以在Razor View上获得View hame。

First is VirtualPath property on View and you can get View Name From path: 首先是View的VirtualPath属性,您可以从路径获取View名称:

@Path.GetFileNameWithoutExtension(Server.MapPath(VirtualPath))

Second is ViewContext but that will work only if you use MVC conversations and call in your controller return View() without explicity passing View name. 第二个是ViewContext但仅当您使用MVC对话并在控制器中调用而未显式传递View名称的情况下return View() ,此函数才有效。

@ViewContext.RouteData.Values["controller"].ToString()

如果使用RazorView ,则可以通过以下代码获取View路径:

(this.ViewContext.View as RazorView).ViewPath

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM