سؤال

أنا أستخدم Html.BeginForm وأحاول تمرير القيمة المتوفرة لمربع النص "archName" إلى المنشور، كيف يمكنني القيام بذلك؟أعني ما الذي يجب أن أضيفه بدلاً من "someString"؟

<% using (Html.BeginForm("addArchive", "Explorer", new { name = "someString" }, FormMethod.Post)) { %> 
    <%=  Html.TextBox("archName")%>
هل كانت مفيدة؟

المحلول

الاسم الذي تشير إليه هو سمة الاسم لعنصر HTML للنموذج، وليس القيم المنشورة.يمكنك الوصول إلى وحدة التحكم بعدة طرق.

مع عدم وجود معلمة في طريقة التحكم:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult addArchive()
{
    string archName = HttpContext.Reqest.Form["archName"]
    return View();
}

مع ال FormCollection كمعلمة في طريقة التحكم:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult addArchive(FormCollection form)
{
    string archName = form["archName"];
    return View();
}

مع بعض النماذج الملزمة:

//POCO
class Archive
{
    public string archName { get; set; }
}

//View
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<Namespace.Archive>" %>    
<%= Html.TextBoxFor(m => m.archName) %>

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult addArchive(Archive arch)
{
    string archName = arch.archName ;
    return View();
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top