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

时间:2010-07-30 20:39:53

标签: c# .net renderaction

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

我有一个返回局部视图的动作:

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

任何想法的人?我很难过,但我确信这很简单,我很想念。

1 个答案:

答案 0 :(得分:0)

我会猜测它只是没有正确设置内容类型。网络跟踪(小提琴等)显示什么?也许尝试使用View("StudentPoll", m);代替PartialView(...)

也;要小心 - 许多<%=看起来不安全,即不是html编码的。