views:

80

answers:

3

Hi I'm using ASP.NET MVC to try to postback after confirmation using javascript, this is the button:

<input type="submit" id ="RemoveStatus" value="Remove Status" name="button" onclick="return CheckRemove();"/>

This is my javascript in my CheckRemove() js function:

var button1 = document.getElementById("RemoveStatus");

     if (confirm("Are you sure you want to remove status?") == true) 
                {
                    button1.disabled = true;
                    button1.value = "Removing status...";
                    __doPostBack('RemoveStatus', '');
                    return true;


                }
                else 
                {
                    return false;
                }

But for some reason I get an object expected error at the __doPostBack bit, I've clearly set the id, button1 gets populated too in debug, i'v tried passing button1.id and button1 too into the __doPostBack call but it wont postback and keeps saying object expected, any ideas?

+1  A: 

You don't have the concept of postbacks in MVC like you had in webforms. Simply use nameoftheform.submit(); instead.

http://www.javascript-coder.com/javascript-form/javascript-form-submit.phtml

You can use the following to generate the id in the form tag:

<% using (Html.BeginForm("Create", "Test", FormMethod.Post, new {id="myForm"})) {%>

and in the script use:

document.getElementById('myForm').submit();

Personally I'm a fan of jQuery so that last one could be rewritten like:

$('#myForm').submit();
XIII
Thanks, do i have to specify name="myform" in the form tag? Or its Id to call submit on it? For example how do I get the name of the form, sorry for the fundamental question - but do I just get it by ID as above?
David
A: 

try doing jquery submit like this

$("#RemoveStatus").submit();

it worked with me

Nikola Markezic
A: 

Use Jquery submit function.

A nice example can be found here Don't forget to do a : return:false; otherwise the form will be post twice: once from the jquery, and once from the button call in the form.

Another nice feature is that in your MVC Action (in your controller) you can catch your Model if you use one. If you bind your model to your textfields etc, you get an updated model, so you don't have to parse your formcollection.

I got some code from a project of mine.

I have a page object that is stored in db.

When I create my view I choose strongly typed-> page object which gives me:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<LIB.Data.Page>" %>

I bind my model to textboxes:

<legend>Fields</legend>
<div class="editor-field">
    <%=Html.TextBoxFor(model => model.Title)%>
</div>

in my controller I do this:

[HttpPost]
    [ValidateInput(false)]
    public ActionResult Create(Page model, FormCollection collection)
    {
        PageService.AddPage((string)Session["lang"], model);
        return RedirectToAction("Index", new { menuGuid = model.MenuGuid });
    }

everything I put in the textfields is bound to the model which I retrieve in the controller and store

Nealv