gpt4 book ai didi

c# - MVC4 如何动态地将行项目添加到 EditorFor 字段?

转载 作者:太空狗 更新时间:2023-10-29 20:39:54 24 4
gpt4 key购买 nike

我有一个包含迭代项的 View 模型。我通过 EditorFor() 方法将它们放在我的 View 中。

查看:

@model Models.MyModel 

@using (Html.BeginForm(@Model.Action, @Model.Controller))
{
<div class="section" id="Terms">
@Html.EditorFor(m => m.Terms)
</div>

<input type="submit" value="Save" />
}

型号:

public class MyModel 
{
public IEnumerable<Term> Terms { get; set; }
}

EditorTemplates\Term.cshtml:

@model Models.Term

@if (Model != null)
{
<fieldset>
<legend>Term</legend>

@Html.HiddenFor(model => model.TermID)

<div class="editor-label">
@Html.LabelFor(model => model.Identifier)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Identifier)
@Html.ValidationMessageFor(model => model.Identifier)
</div>

<div class="editor-label">
@Html.LabelFor(model => model.Description)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Description)
@Html.ValidationMessageFor(model => model.Description)
</div>

</fieldset>
}

我希望能够在 View 的列表中动态添加/删除项目,就像 knockout.js 上的这个示例一样,但是我如何保留自动 ID 的 MVC 创建的??:

http://knockoutjs.com/examples/cartEditor.html

这是我对此的要求:

  • 添加新条款
  • 删除条款
  • 验证添加的新条款 View

我已经阅读了关于 SO 的其他问题,但我还没有找到真正明确的答案。 knockout.js 是公认的方法吗?是否有任何使用 Knockout 和 MVC 执行此操作的示例?

谢谢!

最佳答案

我找到了这篇文章 Nested Collection Models in MVC3由 Jarrett Meyer 提出,他有一个不使用 knockout 并最大化代码重用的解决方案。

这涵盖了添加和删除方法。我将在此处概述添加方法。

模型

public class Person {
public string FirstName { get; set; }
public string LastName { get; set; }
public IList<PhoneNumber> PhoneNumbers { get; set; }
public IList<EmailAddress> EmailAddresses { get; set; }
public IList<Address> Addresses { get; set; }
}

浏览量

//New.cshtml:    
@using (Html.BeginForm("New", "Person", FormMethod.Post))
{
@Html.EditorForModel()
<p>
<button type="submit">
Create Person
</button>
</p>
}

//Person.cshtml:
@Html.AntiForgeryToken()
@Html.HiddenFor(x => x.Id)

<p>
<label>First Name</label>
@Html.TextBoxFor(x => x.FirstName)
</p>
<p>
<label>Last Name</label>
@Html.TextBoxFor(x => x.LastName)
</p>
<div id="phoneNumbers">
@Html.EditorFor(x => x.PhoneNumbers)
</div>
<p>
@Html.LinkToAddNestedForm("Add Phone Number", "#phoneNumbers", ".phoneNumber", "PhoneNumbers", typeof(PhoneNumber))
</p>

//PhoneNumber.cshtml:
<div class="phoneNumber">
<p>
<label>Telephone Number</label>
@Html.TextBoxFor(x => x.Number)
</p>
<br/>
</div>

助手

/// <param name="linkText">Text for Link</param>
/// <param name="containerElement">where this block will be inserted in the HTML using a jQuery append method</param>
/// <param name="counterElement">name of the class inserting, used for counting the number of items on the form</param>
/// <param name="collectionProperty">the prefix that needs to be added to the generated HTML elements</param>
/// <param name="nestedType">The type of the class you're inserting</param>
public static IHtmlString LinkToAddNestedForm<TModel>(this HtmlHelper<TModel> htmlHelper, string linkText,
string containerElement, string counterElement, string collectionProperty, Type nestedType)
{
var ticks = DateTime.UtcNow.Ticks;
var nestedObject = Activator.CreateInstance(nestedType);
var partial = htmlHelper.EditorFor(x => nestedObject).ToHtmlString().JsEncode();

partial = partial.Replace("id=\\\"nestedObject", "id=\\\"" + collectionProperty + "_" + ticks + "_");
partial = partial.Replace("name=\\\"nestedObject", "name=\\\"" + collectionProperty + "[" + ticks + "]");

var js = string.Format("javascript:addNestedForm('{0}','{1}','{2}','{3}');return false;", containerElement,
counterElement, ticks, partial);

TagBuilder tb = new TagBuilder("a");
tb.Attributes.Add("href", "#");
tb.Attributes.Add("onclick", js);
tb.InnerHtml = linkText;

var tag = tb.ToString(TagRenderMode.Normal);
return MvcHtmlString.Create(tag);
}

private static string JsEncode(this string s)
{
if (string.IsNullOrEmpty(s)) return "";
int i;
int len = s.Length;

StringBuilder sb = new StringBuilder(len + 4);
string t;

for (i = 0; i < len; i += 1)
{
char c = s[i];
switch (c)
{
case '>':
case '"':
case '\\':
sb.Append('\\');
sb.Append(c);
break;
case '\b':
sb.Append("\\b");
break;
case '\t':
sb.Append("\\t");
break;
case '\n':
//sb.Append("\\n");
break;
case '\f':
sb.Append("\\f");
break;
case '\r':
//sb.Append("\\r");
break;
default:
if (c < ' ')
{
//t = "000" + Integer.toHexString(c);
string tmp = new string(c, 1);
t = "000" + int.Parse(tmp, System.Globalization.NumberStyles.HexNumber);
sb.Append("\\u" + t.Substring(t.Length - 4));
}
else
{
sb.Append(c);
}
break;
}
}
return sb.ToString();
}

Javascript

//since the html helper can change the text of the item inserted but not the index,
//this replaces the 'ticks' with the correct naming for the collection of properties
function addNestedForm(container, counter, ticks, content) {
var nextIndex = $(counter).length;
var pattern = new RegExp(ticks, "gi");

content = content.replace(pattern, nextIndex);
$(container).append(content);
}

关于c# - MVC4 如何动态地将行项目添加到 EditorFor 字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14699615/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com