views:

60

answers:

2
<script type="text/javascript">
     $('#TextEdit').click(function () {
         $('#ObnAdd').val('Save');
     });
    </script>
    <% using (Html.BeginForm("Create", "ObnTextComponents",FormMethod.Post,new {@id = "TheForm"}))
       {%>

I need to check the condition if my ObnAdd Button text is Add need to go Create ActionResult

if Button text is Save I need to go Save ActionResult..

how to check this Condition in BeginForm?

thanks

+1  A: 

The ASP executes server-side before pageload and has no access to the dom. Whereas the javascript executes client-side during and after pageload.

Since changing the button text is done in javascript (after all the asp runs), the button will always have the same value during pageload, so the branch is unnecessary. Also note that the asp can't access the dom of the page it's creating to test for such things. You would need to either include a library that forms the dom tree for you or use standard text operators to check the value you're looking for (like a regex).

A simple solution to what I think you're doing here would be to maintain a hidden input on the form that is also updated when you update the button. Then you can have the button submit and the page handling the form can make the necessary decisions with all information available.

Anthony DiSanti
PHP? wtf? did u see the tags?
RPM1984
Oh wow, I saw the <% and my php side just took over. I'll update the post.
Anthony DiSanti
+2  A: 

From your comments it seems that it's better to check for the value of the button on the Controller side. Because you can't change your aspx code after the page loads.

So, in your controller you should have something like this (make sure your ObnAdd has name=ObnAdd):

public ActionResult SaveCreate(FormCollection form, string ObnAdd)
{
    if (ObnAdd == "Save")
    {
        //Do save
    }
    else if (ObnAdd == "Create")
    {
        //Do create
    }

    //here return RedirectToAction or whatever
    return RedirectToAction("Index");
}

And your HTML:

<% using (Html.BeginForm("SaveCreate", "ObnTextComponents",FormMethod.Post,new {@id = "TheForm"}))
       {%>
Francisco