.net Html.RenderAction 返回二进制而不是 HTML。啊?

发布于 2024-09-12 09:01:38 字数 4108 浏览 8 评论 0原文

我不确定我在这里做错了什么。

我有一个返回部分视图的操作:

public class SharedController : BaseController
{
public ActionResult StudentPoll()
        {
            WAM.X2O.FuelUpToPlayContext db = new WAM.X2O.FuelUpToPlayContext(WAM.Utilities.Config.ConnectionString);
            WAM.X2O.StudentPoll m = (from s in db.StudentPolls where s.IsActive == true select s).SingleOrDefault();

            return PartialView("StudentPoll", m);
        }
}

我像这样实现该操作:

<%Html.RenderAction("StudentPoll", "Shared");%>

部分视图如下所示:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl< Fuel_Up_To_Play.Models.Shared.StudentPollModel >" %>
<%if(ViewData.Model != null){ %>
<div class="block">
    <div class="holder">
        <div class="frame">
            <h2 class="poll">Student Poll</h2>
            <!-- TODO - Student Poll has a form screen, and results screen. 
                 Results anim is here to demo what should happen after submision -->
            <form action="/Shared/SubmitStudentPoll" method="post" class="poll-form" <%if(ViewData.Model.StartingState == Fuel_Up_To_Play.Models.Shared.StudentPollModel.PollStates.RESULTS){%>style="display:none"<%} %>>
                <fieldset>
                    <span><%=ViewData.Model.StudentPoll.Question %></span>
                    <input type="hidden" id="student_poll" name="student_poll" value="<%=ViewData.Model.StudentPoll.ID %>" />
                    <%foreach(WAM.X2O.StudentPollAnswer answer in ViewData.Model.StudentPoll.RelatedStudentPollAnswers){ %>
                    <div class="row">
                        <input type="radio" class="radio" id="answer_<%=answer.ID %>" name="poll_answer" value="<%=answer.ID %>"/>
                        <label for="answer_<%=answer.ID %>"><%=answer.AnswerText%></label>
                    </div>
                    <%} %>
                    <input id="submitBtn" type="image" style="display:none" class="image" src="/Content/images/btn-submit-05.gif" />
                </fieldset>
            </form>
            <div class="progress-box" <%if(ViewData.Model.StartingState == Fuel_Up_To_Play.Models.Shared.StudentPollModel.PollStates.FORM){%>style="display:none"<%} %>>
                <span><%=ViewData.Model.StudentPoll.Question %></span>
                <%int answerCounter = 0;
                foreach(WAM.X2O.StudentPollAnswer answer in ViewData.Model.StudentPoll.RelatedStudentPollAnswers){ %>
                <div class="box">
                    <span class="names"><%=answer.AnswerText%><strong class="quesPctTxt" rel="<%=ViewData.Model.AnswerPercentages[answerCounter] %>"></strong></span>
                    <div class="progress-bar"><span class="quesPctBar"></span></div>
                </div>
                <%
                    answerCounter++;
                } %>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript" src="/Scripts/studentpollscript.js"></script>
<script type="text/javascript">

    $(document).ready(function() {

        $("input.radio[name='poll_answer']").change(function() {
            $("#submitBtn").show();
        });

        $("#submitBtn").click(function() {

            $(".poll-form").ajaxForm(
                { dataType: 'json',
                    success: function(json) {
                        alert(json.Success);

                    }
                }
            );

        });
    });

</script>

<%} %>

当然,我希望这种方法返回 HTML。但没有。相反,二进制文件似乎正在浏览器中呈现。显然我做错了什么,但我不知道是什么。

这是在浏览器中呈现的内容: http://screencast.com/t/Mjg1OWJj

大家有什么想法吗?我很困惑,但我确信我错过了一些简单的事情。

I'm not sure exactly what I'm doing wrong here.

I have an action which returns a partial view:

public class SharedController : BaseController
{
public ActionResult StudentPoll()
        {
            WAM.X2O.FuelUpToPlayContext db = new WAM.X2O.FuelUpToPlayContext(WAM.Utilities.Config.ConnectionString);
            WAM.X2O.StudentPoll m = (from s in db.StudentPolls where s.IsActive == true select s).SingleOrDefault();

            return PartialView("StudentPoll", m);
        }
}

I implement the action like this:

<%Html.RenderAction("StudentPoll", "Shared");%>

The partial view looks like this:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl< Fuel_Up_To_Play.Models.Shared.StudentPollModel >" %>
<%if(ViewData.Model != null){ %>
<div class="block">
    <div class="holder">
        <div class="frame">
            <h2 class="poll">Student Poll</h2>
            <!-- TODO - Student Poll has a form screen, and results screen. 
                 Results anim is here to demo what should happen after submision -->
            <form action="/Shared/SubmitStudentPoll" method="post" class="poll-form" <%if(ViewData.Model.StartingState == Fuel_Up_To_Play.Models.Shared.StudentPollModel.PollStates.RESULTS){%>style="display:none"<%} %>>
                <fieldset>
                    <span><%=ViewData.Model.StudentPoll.Question %></span>
                    <input type="hidden" id="student_poll" name="student_poll" value="<%=ViewData.Model.StudentPoll.ID %>" />
                    <%foreach(WAM.X2O.StudentPollAnswer answer in ViewData.Model.StudentPoll.RelatedStudentPollAnswers){ %>
                    <div class="row">
                        <input type="radio" class="radio" id="answer_<%=answer.ID %>" name="poll_answer" value="<%=answer.ID %>"/>
                        <label for="answer_<%=answer.ID %>"><%=answer.AnswerText%></label>
                    </div>
                    <%} %>
                    <input id="submitBtn" type="image" style="display:none" class="image" src="/Content/images/btn-submit-05.gif" />
                </fieldset>
            </form>
            <div class="progress-box" <%if(ViewData.Model.StartingState == Fuel_Up_To_Play.Models.Shared.StudentPollModel.PollStates.FORM){%>style="display:none"<%} %>>
                <span><%=ViewData.Model.StudentPoll.Question %></span>
                <%int answerCounter = 0;
                foreach(WAM.X2O.StudentPollAnswer answer in ViewData.Model.StudentPoll.RelatedStudentPollAnswers){ %>
                <div class="box">
                    <span class="names"><%=answer.AnswerText%><strong class="quesPctTxt" rel="<%=ViewData.Model.AnswerPercentages[answerCounter] %>"></strong></span>
                    <div class="progress-bar"><span class="quesPctBar"></span></div>
                </div>
                <%
                    answerCounter++;
                } %>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript" src="/Scripts/studentpollscript.js"></script>
<script type="text/javascript">

    $(document).ready(function() {

        $("input.radio[name='poll_answer']").change(function() {
            $("#submitBtn").show();
        });

        $("#submitBtn").click(function() {

            $(".poll-form").ajaxForm(
                { dataType: 'json',
                    success: function(json) {
                        alert(json.Success);

                    }
                }
            );

        });
    });

</script>

<%} %>

Natually, I would expect this approach to return HTML. But no. Instead it appears that binary is being rendered in the browser. Obviously I'm doing something wrong but I don't know what.

Here's what is rendered in the browser:
http://screencast.com/t/Mjg1OWJj

Any ideas folks? I'm stumped but I'm sure it's something simple that I'm missing.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

緦唸λ蓇 2024-09-19 09:01:38

我猜测它根本没有正确设置内容类型。网络跟踪(fiddler 等)显示什么?也许尝试使用 View("StudentPoll", m); 而不是 PartialView(...)

还;请小心 - 其中许多 <%= 看起来不安全,即不是 html 编码的。

I would guess that it simply isn't setting the content type correctly. What does a network trace (fiddler, etc) show? Perhaps try using View("StudentPoll", m); instead of PartialView(...)?

Also; be careful - many of those <%= look unsafe, i.e. not html-encoded.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文