سؤال

لدي زران في نموذج MVC الخاص بي:

<input name="submit" type="submit" id="submit" value="Save" />
<input name="process" type="submit" id="process" value="Process" />

من إجراء جهاز التحكم الخاص بي كيف أعرف أي واحد تم الضغط عليه؟

هل كانت مفيدة؟

المحلول

اسم كل من أزرار تقديم الخاصة بك هو نفسه

<input name="submit" type="submit" id="submit" value="Save" />
<input name="submit" type="submit" id="process" value="Process" />

ثم في وحدة التحكم الخاصة بك الحصول على قيمة الإرسال. فقط الزر النقر سوف يمر بقيمته.

public ActionResult Index(string submit)
{
    Response.Write(submit);
    return View();
}

يمكنك بالتأكيد تقييم هذه القيمة لأداء عمليات مختلفة مع كتلة التبديل.

public ActionResult Index(string submit)
{
    switch (submit)
    {
        case "Save":
            // Do something
            break;
        case "Process":
            // Do something
            break;
        default:
            throw new Exception();
            break;
    }

    return View();
}

نصائح أخرى

<input name="submit" type="submit" id="submit" value="Save" />
<input name="process" type="submit" id="process" value="Process" />

وفي إجراء جهاز التحكم الخاص بك:

public ActionResult SomeAction(string submit)
{
    if (!string.IsNullOrEmpty(submit))
    {
        // Save was pressed
    }
    else
    {
        // Process was pressed
    }
}

هذه إجابة أفضل، حتى نتمكن من الحصول على النص والقيمة لكل من زر:

http:/weblogs.asp.net/dfindley/archive/2009/05/31/asp-net-mvc-multiple-buttons-same-same-form.aspx.

</p>
<button name="button" value="register">Register</button>
<button name="button" value="cancel">Cancel</button>
</p>

وحكم المراقب:

public ActionResult Register(string button, string userName, string email, string password, string confirmPassword)
{
if (button == "cancel")
    return RedirectToAction("Index", "Home");
...

باختصار زر إرسال ولكن يمكنك اختيار الاسم باستخدام سمة الاسم، وهو أكثر قوة لأنك غير ملزم بإرسال الاسم أو الزر في معلمات أسلوب وحدة التحكم، يمكنك الاتصال به كما تريد ...

يمكنك تحديد الزر الخاص بك من هناك علامة اسم مثل أدناه، تحتاج إلى التحقق من ذلك مثل وحدة التحكم فيك

if (Request.Form["submit"] != null)
{
//Write your code here
}
else if (Request.Form["process"] != null)
{
//Write your code here
}

إليك طريقة لطيفة حقا وبسيطة للقيام بذلك من السهل حقا اتباع التعليمات باستخدام MultiTTonTtonAttrive مخصص:

http://blog.maartenballiauw.be/post/2009/11/26/supporting-multiple-submit-buttons-on-an-aspnet-mvc-view.aspx.

لتلخيص، اجعل أزرار تقديمك مثل هذا:

<input type="submit" value="Cancel" name="action" />
<input type="submit" value="Create" name="action" /> 

أفعالك مثل هذا:

[HttpPost]
[MultiButton(MatchFormKey="action", MatchFormValue="Cancel")]
public ActionResult Cancel()
{
    return Content("Cancel clicked");
}

[HttpPost]
[MultiButton(MatchFormKey = "action", MatchFormValue = "Create")]
public ActionResult Create(Person person)
{
    return Content("Create clicked");
} 

وإنشاء هذه الفئة:

[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public class MultiButtonAttribute : ActionNameSelectorAttribute
{
    public string MatchFormKey { get; set; }
    public string MatchFormValue { get; set; }

    public override bool IsValidName(ControllerContext controllerContext, string actionName, MethodInfo methodInfo)
    {
        return controllerContext.HttpContext.Request[MatchFormKey] != null &&
            controllerContext.HttpContext.Request[MatchFormKey] == MatchFormValue;
    }
}
// Buttons
<input name="submit" type="submit" id="submit" value="Save" />
<input name="process" type="submit" id="process" value="Process" />

// Controller
[HttpPost]
public ActionResult index(FormCollection collection)
{
    string submitType = "unknown";

    if(collection["submit"] != null)
    {
        submitType = "submit";
    }
    else if (collection["process"] != null)
    {
        submitType = "process";
    }

} // End of the index method

لتسهيل الأمر، سأقول أنه يمكنك تغيير الأزرار الخاصة بك بما يلي:

<input name="btnSubmit" type="submit" value="Save" />
<input name="btnProcess" type="submit" value="Process" />

وحدة تحكم الخاصة بك:

public ActionResult Create(string btnSubmit, string btnProcess)
{
    if(btnSubmit != null)
       // do something for the Button btnSubmit
    else 
       // do something for the Button btnProcess
}

لن يجيب هذا المنشور على كوبرميل، لأنه تم الرد عليه منذ وقت طويل. مشاركتي ستكون مفيدة لمن سيسعى للحصول على حل مثل هذا. بادئ ذي بدء، يجب أن أقول "حل WDUFFY صحيح تماما" ويعمل بشكل جيد، ولكن سيتم استخدام حلاي (ليس في الواقع) في عناصر أخرى ويجعل طبقة العرض التقديمي أكثر استقلالية من وحدة التحكم (لأن وحدة التحكم الخاصة بك تعتمد عليها "القيمة" التي يتم استخدامها لإظهار تسمية الزر، هذه الميزة مهمة لغات أخرى.).

إليك حلاي، ومنحهم أسماء مختلفة:

<input type="submit" name="buttonSave" value="Save"/>
<input type="submit" name="buttonProcess" value="Process"/>
<input type="submit" name="buttonCancel" value="Cancel"/>

ويجب أن تحدد أسماء الأزرار كوسائط في الإجراء مثل أدناه:

public ActionResult Register(string buttonSave, string buttonProcess, string buttonCancel)
{
    if (buttonSave!= null)
    {
        //save is pressed
    }
    if (buttonProcess!= null)
    {
        //Process is pressed
    }
    if (buttonCancel!= null)
    {
        //Cancel is pressed
    }
}

عندما يقدم المستخدم الصفحة باستخدام أحد الأزرار، ستكون واحدة فقط من الحجج قيمة. أعتقد أن هذا سيكون مفيدا للآخرين.

تحديث

هذه الإجابة قديمة جدا وأنا في الواقع إعادة النظر في رأيي. ربما يكون الحل أعلاه هو جيد للموقف الذي يمر المعلمة خصائص النموذج. لا تزعج أنفسكم وتأخذ أفضل حل لمشروعك.

امنح الاسم إلى كل من الأزرار واحصل على التحقق من القيمة من النموذج.

<div>
   <input name="submitButton" type="submit" value="Register" />
</div>

<div>
   <input name="cancelButton" type="submit" value="Cancel" />
</div>

على جانب تحكم:

public ActionResult Save(FormCollection form)
{
 if (this.httpContext.Request.Form["cancelButton"] !=null)
 {
   // return to the action;
 }

else if(this.httpContext.Request.Form["submitButton"] !=null)
 {
   // save the oprtation and retrun to the action;
 }
}

في الصفحات الحلاقة الأساسية 2.2 أعمال بناء الجملة هذه:

    <button type="submit" name="Submit">Save</button>
    <button type="submit" name="Cancel">Cancel</button>
public async Task<IActionResult> OnPostAsync()
{
    if (!ModelState.IsValid)
        return Page();
    var sub = Request.Form["Submit"];
    var can = Request.Form["Cancel"];
    if (sub.Count > 0)
       {
       .......

هل لا يمكنك معرفة طلب الطلب؟ إذا تم النقر فوق العملية، فلن يكون الطلب ["العملية"] فارغة

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top