8

Im Facing a weird problem on my JQuery mobile APP in MVC4:

I have a form with a couple of texboxes like this

@using Models
@model Models.DataModel.Pagina_Details
@{
Layout = "~/Views/Shared/_Layout.cshtml";
}


@using (Html.BeginForm("Pagina_Details", "Home", FormMethod.Post, new { id =          "PaginaDetailsForm" }))
{
if (Request.QueryString["Command"] != null)
{
    Session["Command"] = Request.QueryString["Command"];
}
else
{
    Response.Redirect("Index");
}
<div class="ui-corner-all custom-corners ui-shadow">
  <div class="ui-bar ui-bar-a">
    <h3>Pagina's</h3>
  </div>
<div class="ui-body ui-body-a">
 <input type="hidden" id="Command" />
  @Html.HiddenFor(model => model.ID)
 @Html.LabelFor(model => model.Name, "Naam *", new { @class = "lbl"})    
 @Html.TextBoxFor(model => model.Name, new { required = "required" })

 @Html.LabelFor(model => model.Description, "Omschrijving *", new { @class = "lbl"})    
 @Html.TextArea("Description", new { required = "required", rows="10", cols="80"})

 @Html.LabelFor(model => model.MetaDescription, "Meta description", new { @class =  "lbl" })    
 @Html.TextBoxFor(model => model.MetaDescription)

 @Html.LabelFor(model => model.MetaKeywords, "Meta keywords", new { @class = "lbl" })    
 @Html.TextBoxFor(model => model.MetaKeywords)

 @Html.LabelFor(model => model.Active, "Actief", new { @class = "lbl" })   
 @Html.CheckBoxFor(model => model.Active)

@if (Session["Command"] == "Insert" || Request.QueryString["Command"] == "Insert")
{
    <button type="submit" name="Command" data-role="button" data-icon="plus">Toevoegen</button>
}       
@if (Session["Command"] != "Insert" && Request.QueryString["Command"] != "Insert")
{
    <button type="submit" id="Edit" name="Command" value="Opslaan" data-role="button" data-icon="edit">Opslaan</button>
    <button type="submit" id="Verwijderen" name="Command" value="Verwijderen" data-role="button" data-icon="delete">Verwijderen</button>
}

</div>

</div>

}

in my ActionResult (Controller) i have the param Command and use that in a switch to do something with it the issue is on the desktop browser it works well and i can see the command is passing to the ActionResult and everything works fine as it should be but for some reason when i try the same thing on my Mobile phone with Phonegap the Command value will always be null

What i tryed: AttributeUsage How do you handle multiple submit buttons in ASP.NET MVC Framework? no result at all.

also i tried different ActionResults for the 2 buttons no result at all.

im lost does someone knows some tips or have any ideas how i can fix this. ty for your time and help.

Community
  • 1
  • 1
Stefan van de Laarschot
  • 2,154
  • 6
  • 30
  • 50

2 Answers2

1

This might not be what your looking for but why not use the buttons as javascript functions which could change the action of the form before submitting. I think this is a nice way to keep your logic clean and a simple fix. Since your using phonegap javascript shouldnt be a problem. Good Luck!

<script type="text/javascript">
document.getElementById("edit").onclick = function() {
 a=document.getElementsByTagName("form")[0];
a.action ="URL for Edit";
a.submit();
 }
document.getElementById("Verwijderen").onclick = function() {
 a=document.getElementsByTagName("form")[0];
a.action ="URL for Verwijderen";
a.submit();
 }

</script>

Hope This Helps! -Drew

  • No thats not what i want since its all the same action (ActionResult) but thanks for your ideas :) – Stefan van de Laarschot May 30 '14 at 12:26
  • Darn lol that all I got. My question would be if all of the logic has to take place in one action result. If you broke it up into 3 you could use two to just pass variables to the 1 doing all the work. This is just an option but i would think breaking up logic helps with organization. – DrewGoldsberry May 30 '14 at 22:02
  • Hey I reproduced this answer. The second one is easier. http://stackoverflow.com/questions/442704/how-do-you-handle-multiple-submit-buttons-in-asp-net-mvc-framework – DrewGoldsberry May 31 '14 at 04:59
  • When i use passing variable like public ActionResult(Model D, String Command) { if(Command == "A") { //Do Something } } it is working on my Desktop PC but not on my mobile device thats all ? and i dont know why it isnt working on my mobile platform (Google Chrome):( – Stefan van de Laarschot May 31 '14 at 08:05
1

Try this :

If you want to use multiple submit button then you have to use pass FormCollection Form parameter to the post action like this

First change your input button name it cant be same if you want to use multiple submit button so change your submit button code to like this :

@if (Session["Command"] != "Insert" && Request.QueryString["Command"] != "Insert")
{
    <button type="submit" id="Edit" name="CommandBtn1" value="Opslaan" data-role="button" data-icon="edit">Opslaan</button>
    <button type="submit" id="Verwijderen" name="CommandBtn2" value="Verwijderen" data-role="button" data-icon="delete">Verwijderen</button>
}

Now On Home controller post event of the action Pagina_Details is written as :

[HttpPost] 
public ActionResult Pagina_Details(FormCollection Form,ModelClassName ModelClassObject)
{
if(Form["CommandBtn1"]!=null)
{
/// Write code for Opslaan (Edit Code)
}
if(Form["CommandBtn2"]!=null)
{
/// Write code for Verwijderen (Delete Code)
}
return View();
} 

Hopefully its works.

Amit
  • 823
  • 7
  • 13