如何在MVC 3中调用页面 [英] How to call a page in MVC 3

查看:53
本文介绍了如何在MVC 3中调用页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我在一个MVC 3应用程序中工作,该应用程序有一个MVC View页面,即PlanMyTravel.csthml。



这个页面没有被函数@RenderObject()调用。



如何使用它调用我的页面。



下面你可以看到PlanMyTravel.cshtml文件:



Hi,

I am working in an MVC 3 application which is having a MVC View Page, namely PlanMyTravel.csthml.

This page is not invoking by the function @RenderObject().

How to call my the page using that.

Below you can see PlanMyTravel.cshtml file:

@model MvcDemo.Models.PlanMyTravel

@{
    ViewBag.Title = "Plan My Travel";
}

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

@using (Html.BeginForm()) {
    <div>
        <fieldset>
            <legend>Plan My Travel</legend>

            <div class="editor-label">
                @Html.LabelFor(m => m.From)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(m => m.From)
                @Html.ValidationMessageFor(m => m.From)
            </div>

            <div class="editor-label">
                @Html.LabelFor(m => m.To)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(m => m.To)
                @Html.ValidationMessageFor(m => m.To)
            </div>

            <div class="editor-label">
                @Html.LabelFor(m => m.Date)            
            </div>
             

            <div class="editor-label">
                @Html.TextBoxFor(m => m.Date)
                
            </div>

            <p>
                <input type="submit" value="Find Trains" />
                <input type="submit" value ="Reset" />
            </p>
        </fieldset>
    </div>
}





在这里你可以找到_Layout.cshtml文件。



Here you can find _Layout.cshtml file.

<!DOCTYPE html>
<html>
<head>
    <title>@ViewBag.Title</title>
    <link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
    <script src="@Url.Content("~/Scripts/jquery-1.4.4.min.js")" type="text/javascript"></script>
</head>

<body>
    <div class="page">
        <div id="header">
            <div id="title">
              <h1>Indian Railway Reservation</h1>      
            </div>

            <div id="logindisplay">
            @Html.Partial("_LogOnPartial")
            </div>
 
            <div id="menucontainer">

                <ul id="menu">
                    <li>@Html.ActionLink("Home", "Index", "Home")</li>
                    <li>@Html.ActionLink("About", "About", "Home")</li>
                </ul> 
            </div>
        </div>

        <div id="main">
            @RenderBody()
            <div id="footer">
            </div>
        </div>
    </div>
</body>
</html

推荐答案

请看我对这个问题的评论。



让我详细说明一下:当然,在你的服务器上在代码中,您可以通过读取HTML文件来加载某些页面,或者说,通过使用 System.Net.HttpWebRequest (然后它可能是某些ASP.NET页面)因为它本身将由服务器端a准备胀。即使您这样做,您也将获得包含一些HTML内容的页面。如果您只是想将此HTML插入 div ,它将不会呈现合法的HTML DOM结构: html 元素不能是 div 元素的孩子,它不能是任何东西的子元素。



Of当然,你可以解析这个HTML,提取它的一些内部部分,然后插入它,但它真的会破坏ASP.NET的目的。它确实可行,但进入它不会有任何实际意义。相反,您应该使用ASP.NET机制本身。例如,您可以加载一个可用作某些HTML模板的文件,可能是XML格式,用一些数据填充它,并为 div 。但是以同样的方式,你可以在 div 中使用所有这些结构,并使用一些代码来填充一些数据。此外,您可以使用 System.Web.HttpResponse 从头开始填充此元素。你需要利用ASP.NET的强大功能,而不是反对它。



-SA
Please see my comment to the question.

Let me elaborate on this: of course, in your server-side code, you can load some page either by reading HTML file, or, say, by using System.Net.HttpWebRequest (and then it could be some ASP.NET page as then it itself will be prepared by the server side as well. Even if you do it, you will obtain the page with some HTML content. If you simply want to insert this HTML into div, it won't render a legitimate HTML DOM structure: html element cannot be a child of div element, and it cannot be a child element of anything at all.

Of course, you could parse this HTML, extract some inner part of it, and then insert it, but it would really defeat the purpose of ASP.NET. It really may work, but going into it would not make any practical sense. Instead, you should use ASP.NET mechanism itself. For example, you can load a file which would work as some HTML template, possibly in the form of XML, populate it with some data, and generate some valid inner HTML for the div. But in the same way, you could have all this structure already prescribed in div, and use some code behind to populate it with some data. Also, you can populate this element from scratch, using System.Web.HttpResponse. You need to harness the power of ASP.NET, not to work against it.

—SA


这篇关于如何在MVC 3中调用页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆