簡體   English   中英

如何在MVC 4剃須刀中獲取視圖名稱

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

我想讀取一個活動的View名稱並將其填充到ViewName public Class中

我制作了一個模型類,在其中獲取textBox數據

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

在布局中,我已經在表單標簽中編寫了代碼,這是在其余公共字符串類的發送文本框值

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

我現在想做的是,我還想讀取活動視圖名稱,以填充ViewName公共類,以便使用相同的視圖再次將其發回布局中。 你們知道我如何閱讀viewName並將其填寫到Public Class中嗎? 請讓我知道在哪里可以在Form標簽中添加此內容。

先感謝您

有兩種可能的方法可以在Razor View上獲得View hame。

首先是View的VirtualPath屬性,您可以從路徑獲取View名稱:

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

第二個是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