كيفية تمرير القائمة كمعلمة في url.action؟

برمجة


هل يمكن لأي أحد أن يساعدني في كيفية تمرير قائمة القيم الصحيحة من العرض إلى وحدة التحكم MVC باستخدام url.action؟؟

الحل 2

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

XML
@{
    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>

تحديث:

XML
@{
    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

الحل 3

يمكنك استخدام

@Url.Action("Index", "home", new { data1 = 0, data2 = 0 })
SQL
public ActionResult Index(int data1, int data2)
{
    return View();
}

أو

@Url.Action("Index", "home", new { data = yourintlist })
SQL
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 });

-KR

الحل 5

بدلاً من ذلك، اكتب Casting to “object” أو “object[]” أو باستخدام RouteValueDictionary. هناك طريقة بسيطة لتحقيق ذلك وهي استخدام “Newtonsoft.Json”

في حالة استخدام .Net Core 3.0 أو إصدار أحدث؛

الافتراضي هو استخدام تطبيق المحلل اللغوي System.Text.Json المدمج.

جافا سكريبت
@using System.Text.Json;

…….

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

إذا توقفت عن استخدام .Net Core 2.2 أو إصدار سابق؛

الافتراضي لاستخدام Newtonsoft JSON.Net كخيارك الأول JSON Parser.

جافا سكريبت
@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をコピーしました