अपने विचार से गड़बड़ न करने के लिए, मुझे यह करने का सबसे आसान तरीका इस एक्सटेंशन का पालन करना है: BeginCollectionItem।
पूरा परियोजना यहाँ है: https://github.com/danludwig/BeginCollectionItem
लेकिन AFAIK आप केवल इस वर्ग की जरूरत है:
public static class HtmlPrefixScopeExtensions
{
private const string idsToReuseKey = "__htmlPrefixScopeExtensions_IdsToReuse_";
public static IDisposable BeginCollectionItem(this HtmlHelper html, string collectionName)
{
var idsToReuse = GetIdsToReuse(html.ViewContext.HttpContext, collectionName);
string itemIndex = idsToReuse.Count > 0 ? idsToReuse.Dequeue() : Guid.NewGuid().ToString();
// autocomplete="off" is needed to work around a very annoying Chrome behaviour whereby it reuses old values after the user clicks "Back", which causes the xyz.index and xyz[...] values to get out of sync.
html.ViewContext.Writer.WriteLine(string.Format("<input type=\"hidden\" name=\"{0}.index\" autocomplete=\"off\" value=\"{1}\" />", collectionName, html.Encode(itemIndex)));
return BeginHtmlFieldPrefixScope(html, string.Format("{0}[{1}]", collectionName, itemIndex));
}
public static IDisposable BeginHtmlFieldPrefixScope(this HtmlHelper html, string htmlFieldPrefix)
{
return new HtmlFieldPrefixScope(html.ViewData.TemplateInfo, htmlFieldPrefix);
}
private static Queue<string> GetIdsToReuse(HttpContextBase httpContext, string collectionName)
{
// We need to use the same sequence of IDs following a server-side validation failure,
// otherwise the framework won't render the validation error messages next to each item.
string key = idsToReuseKey + collectionName;
var queue = (Queue<string>)httpContext.Items[key];
if (queue == null) {
httpContext.Items[key] = queue = new Queue<string>();
var previouslyUsedIds = httpContext.Request[collectionName + ".index"];
if (!string.IsNullOrEmpty(previouslyUsedIds))
foreach (string previouslyUsedId in previouslyUsedIds.Split(','))
queue.Enqueue(previouslyUsedId);
}
return queue;
}
private class HtmlFieldPrefixScope : IDisposable
{
private readonly TemplateInfo templateInfo;
private readonly string previousHtmlFieldPrefix;
public HtmlFieldPrefixScope(TemplateInfo templateInfo, string htmlFieldPrefix)
{
this.templateInfo = templateInfo;
previousHtmlFieldPrefix = templateInfo.HtmlFieldPrefix;
templateInfo.HtmlFieldPrefix = htmlFieldPrefix;
}
public void Dispose()
{
templateInfo.HtmlFieldPrefix = previousHtmlFieldPrefix;
}
}
}
कैसे अपने दृश्य में इसका उपयोग करना:
<form method="post" action="/Home/Create">
@foreach (var item in Model.Products) {
@using (Html.BeginCollectionItem("Products"))
{
@Html.TextBoxFor(item => item.Name)
@Html.TextBoxFor(item => item.Price)
}
}
...
...
</form>
मैं इस बारे में सोच आपके विचारों में इंडेक्स के साथ गड़बड़ करने से क्लीनर है ... यहां वह पोस्ट है जो बताती है कि इसे कैसे चरणबद्ध करना है ईपी: http://blog.stevensanderson.com/2010/01/28/editing-a-variable-length-list-aspnet-mvc-2-style/
Nuget पैकेज:http://www.nuget.org/packages/BeginCollectionItem/
यह अभी भी मदों की एक अनुक्रमिक सूची है। वैकल्पिक इंडेक्सिंग एचटीएमएल उत्पन्न करने से परे आपको जो हासिल करने की कोशिश कर रहे हैं, उसके बारे में आपको अधिक संदर्भ प्रदान करने की आवश्यकता है। क्या आप संग्रह आइटम रीडरिंग, अतिरिक्त और निष्कासन को कार्यान्वित करने की कोशिश कर रहे हैं, आपको इस वाक्यविन्यास की आवश्यकता नहीं है? –