开发者

Javascript onclick _dopostback in MVC not posting back

开发者 https://www.devze.com 2023-01-08 07:42 出处:网络
NET MVC to try to postback after confirmation using javascript, this is the button: &l开发者_StackOverflow社区t;input type=\"submit\" id =\"RemoveStatus\" value=\"Remove Status\" name=\"button\" oncl

NET MVC to try to postback after confirmation using javascript, this is the button:

&l开发者_StackOverflow社区t;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?


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();


try doing jquery submit like this

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

it worked with me


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

0

精彩评论

暂无评论...
验证码 换一张
取 消