url.action में सूची को पैरामीटर के रूप में कैसे पास करें?

प्रोग्रामिंग


क्या कोई मेरी मदद कर सकता है कि url.action का उपयोग करके दृश्य से mvc नियंत्रक तक पूर्णांक मानों की सूची कैसे पास की जाए?

समाधान 2

सी#
public ActionResult Index(string[] data)
{
    return View();
}

एक्सएमएल
@{
    var qs = HttpUtility.ParseQueryString("");
    new List<string>{"one", "two", "three"}.ForEach(s => qs.Add("data", s));
}
<a href="@Url.Action("Index", "Home")?@qs">click</a>

अद्यतन:

एक्सएमएल
@{
    ViewBag.name = "xyz";
    ViewBag.data = new List<string>{"one", "two", "three"};

    var q = HttpUtility.ParseQueryString("");
    q.Add("name", ViewBag.name);

    foreach (var item in ViewBag.data)
    {
        q.Add("data", item.ToString());
    }

}
<a href="@Url.Action("Index", "Home")?@q">click</a>

समाधान 1

आप नीचे दिए गए लिंक से जांच कर सकते हैं
http://stackoverflow.com/questions/21785424/how-can-i-use-url-action-with-list-parameters[^]

समाधान 3

आप उपयोग कर सकते हैं

@Url.Action("Index", "home", new { data1 = 0, data2 = 0 })
एसक्यूएल
public ActionResult Index(int data1, int data2)
{
    return View();
}

या

@Url.Action("Index", "home", new { data = yourintlist })
एसक्यूएल
public ActionResult Index(int[] data)
{
    return View();
}

समाधान 4

आपको उपयोग करने की आवश्यकता है & सूची के प्रत्येक आइटम के लिए.
कुछ इस तरह,
ids=1&ids=2&ids=3

सी#
string param = string.empty;
foreach(var item in list)
{
    param += "ids=" + item + "&";
}
param = param.Remove(param.Length - 1); // remove the last appended &
@Url.Action("Action", "Controller", new { data = param });

-के.आर

समाधान 5

इसके बजाय कास्टिंग टू “ऑब्जेक्ट” या “ऑब्जेक्ट” टाइप करें[]या RouteValueDictionary का उपयोग कर रहे हैं। इसे प्राप्त करने का एक सरल तरीका “न्यूटनसॉफ्ट.जेसन” का उपयोग करना है

यदि .नेट कोर 3.0 या बाद का उपयोग कर रहे हैं;

अंतर्निहित System.Text.Json पार्सर कार्यान्वयन का उपयोग करने के लिए डिफ़ॉल्ट।

जावास्क्रिप्ट
@using System.Text.Json;

…….

@Url.Action("ActionName", "ControllerName", new {object = JsonConvert.SerializeObject(‘@ModalObject’)  }))

यदि आप .Net Core 2.2 या इससे पहले के संस्करण का उपयोग कर रहे हैं;

अपनी पहली पसंद JSON पार्सर के रूप में Newtonsoft JSON.Net का उपयोग करने के लिए डिफ़ॉल्ट।

जावास्क्रिप्ट
@using Newtonsoft.Json;

…..

@Url.Action("ActionName", "ControllerName", new {object = JsonConvert.SerializeObject(‘@ModalObject’)  }))

आपको पहले पैकेज इंस्टॉल करने की आवश्यकता हो सकती है.

PM> Install-Package Newtonsoft.Json

तब,

सी#
public ActionResult ActionName(string modalObjectJSON)
{
	Modal modalObj = new Modal();

	modalObj = JsonConvert.DeserializeObject<Modal>(modalObjectJSON);
	
}

コメント

タイトルとURLをコピーしました