قوالب ASP.NET MVC 2 واجهة المستخدم التي تعرض البيانات دون ترميز. كيف إصلاح؟

StackOverflow https://stackoverflow.com/questions/2300980

سؤال

باستخدام Editorfor (Model Lambda ، "ViewTemplatename") ، فإن مخرجاتي ليست كما هو متوقع. هذا لا ينتج عن أي أخطاء ، ولكنه يقدم الإخراج دون ترميز. ما الخطأ الذي افعله؟

الإخراج:

HarryTomRichard

الإخراج المتوقع (أحتاج إلى معرفة كيفية تقديم القائمة [] الفهارس على المعرف أيضًا ولكن ليس لهذه المشكلة حتى الآن):

<table>
    <tr><td><span><input type="Text" id="Name[0]" value="Harry" /></span></td></tr>
    <tr><td><span><input type="Text" id="Name[1]" value="Tom" /></span></td></tr>
    <tr><td><span><input type="Text" id="Name[2]" value="Richard" /></span></td></tr>
</table>

فصولي:

namespace Marcs.Models {
    public class Student   { public string Name { get; set; } }
    public class Classroom { public List<Student> Students { get; set; }
}

وحدة التحكم الخاصة بي:

public ActionResult Index() {
    var myStudents = new List<Student>();
    myStudents.Add(new Student { Name = "Harry" });
    myStudents.Add(new Student { Name = "Tom" });
    myStudents.Add(new Student { Name = "Richard" });
    var myClass = new Classroom {Students = myStudents};
    return View(myClass);
}

عرض الفهرس الخاص بي:

Inherits="System.Web.Mvc.ViewPage<Marcs.Models.Classroom>" %>
<% using (Html.BeginForm()) { %>
    <%= Html.EditorFor(m => m.Students, "Classroom") %>
    <input type="submit" value="Save" />
<% } %>

قالب الفصل الدراسي الخاص بي (لاحظ العنصر m => حتى أتمكن من استخدام العنصر ، وليس النموذج):

Inherits="System.Web.Mvc.ViewUserControl<List<Marcs.Models.Student>>" %>
<table>
    <% foreach (Marcs.Models.Student item in Model)
    { %><tr><td><%= Html.EditorFor(m => item, "Student")%></td></tr><%
    } %>
</table>

قالب الطالب الخاص بي:

Inherits="System.Web.Mvc.ViewUserControl<Marcs.Models.Student>"
%><span><%= Html.Encode( Html.EditorFor( m => m.Name)) %></span>
هل كانت مفيدة؟

المحلول

JFAR لديه الإجابة ، وسأضع علامة عليه بشكل مناسب عند إضافته. كان الحل ببساطة هو التأكد من وجود الملفات في View-> controlname-> editortemplates و Views-> controlname-> displayTemplates. يمكن أن تكون هذه موجودة أيضًا في المجلد المشترك أيضًا.

أنا أحب هذا المنشور. الآن أحتاج إلى معرفة كيفية استخدام مساعدي HTML قالب MVC 2 الذين يشيرون إلى المجموعات. إنه في MVC 2 RC.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top