简体   繁体   English

在SharePoint应用程序页面.aspx上使用模型

[英]Use model on SharePoint application page .aspx

I am working on SharePoint to create a Feedback questionnaire form using an application page that is basically a aspx page. 我正在SharePoint上使用基本上是aspx页面的应用程序页面创建“反馈调查表”表单。

I wish to do this by emulating MVC as far as possible. 我希望通过尽可能模拟MVC来做到这一点。 I've set up my model in the code-behind: 我在后面的代码中建立了我的模型:

public List<QuestionViewModel> modelQuestions = new List<QuestionViewModel>();

Next, I need to display each question and an appropriate input depending on the question type (eg single line, multi line, single selection, multiple selection). 接下来,我需要根据问题类型(例如,单行,多行,单选,多选)显示每个问题和适当的输入。

I've got it displaying the questions correctly: 我已经正确显示了问题:

<fieldset class="what-went-wrong">
    <% for (int i = 0; i < modelQuestions.Count; i++) { %>
        <p>
            <label for="QuestionText">
                <% if (modelQuestions[i].Required) { %>
                <span class="req-field indicator">*</span>
                <% } %>
                    <%= modelQuestions[i].QuestionText %>
                <% if (modelQuestions[i].Required) { %>
                <span class="req-field right">* Required field</span>
                <% } %>
            </label>
        </p>
    <% } %>
</fieldset>

This give's me the question text. 这是问题文本。 I'm now trying to construct the appropriate input, but this <% %> tags is not working for this: 我现在正在尝试构造适当的输入,但是此<% %>标记对此无效:

<% if(modelQuestions[i].QuestionTypeId == QuestionType.SingleLine) { %>
    <input id="modelQuestions_<% i %>" name="modelQuestions[<% i %>]" type="text" placeholder="<% modelQuestions[i].Placeholder %>" />
<% } %>

I can't seem to get it to construct the html element using details from the model (in the value for id, name, placeholder etc.) 我似乎无法使用模型中的详细信息(在ID,名称,占位符等的值中)构造html元素。

Also, I've no idea how to go about posting this back to the server when I get to that point. 另外,我不知道该如何将其发布回服务器。

Is there any merit in continuing? 继续下去有什么好处吗? Are there other controls/methods more appropriate to use in this case with aspx? 在这种情况下,是否还有其他控件/方法更适合与aspx一起使用?

You cannot generate HTML markup like this. 您无法生成这样的HTML标记。 Even data-binding expressions will not help, because they bind ASP.NET controls' attributes values, not the plain output HTML in the page. 甚至数据绑定表达式也无济于事,因为它们绑定ASP.NET控件的属性值,而不绑定页面中的纯HTML输出。

You should generate the markup in the "code behind" , like this: 您应该在“代码背后”中生成标记,如下所示:

Page markup: 页面标记:

<div id='AnswersPanel'>
<div>

Page code behind: 页面代码后面:

protected void PageLoad(...)
{
    AnswersPanel.InnerHtml = "";
    AnswersPanel.InnerHtml += string.Format("<input id='modelQuestions_{0}' name='modelQuestions[{0}]' type='text' placeholder='{1}' />",
                                                i.ToString(), 
                                              modelQuestions[i].Placeholder);
}

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

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