0

When you generate MVC5 application you have in the controller 2 create action like the following

   //   GET: Roles/Create
    public ActionResult Create()
    {
        return View();
    }

and this which is the actual action for post

[HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create([Bind(Include = "Id,name,checkBox1,checkBox2,checkBox3")] Role role)
    {
        if (ModelState.IsValid)
        {
            db.Roles.Add(role);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        return View(role);
    }

I want to call to the second when I press on click from the index view, how should I do that? I try to put this code in the index view which is the button save from the create view but its not working. Any idea?

<div class="form-group">
    <div class="col-md-offset-2 col-md-10">
        <input type="submit" value="Save" class="btn btn-default" />
    </div>
</div> 

Update

Currently I put the button on element like this (at the end of string this is the create button but I see it on the UI but when I click on it noting happen

var html = '<tr><td>@Html.TextBox("Name")</td><td>@Html.CheckBox("checkBox1")</td><td></td><td><input type="submit" value="Create" class="btn bt

This is my all view code

<script src="../../Scripts/jquery-1.10.2.min.js"></script>



<script>

    $(document).ready(function () {






    //Append new row



    var html = '<tr><td>@Html.TextBox("name")</td><td>@Html.CheckBox("checkBox1")</td><td>@Html.CheckBox("checkBox2")</td><td>@Html.CheckBox("checkBox3")</td><td><input id="btnsubmit" type="submit" value="Create" class="btn btn-default" /></td><td></tr>';


</script>



<input type="button" value="Add Row" onclick="addRow()" class="data-button" id="add-row" />


<table class="table" >
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.name)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.checkBox1)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.checkBox2)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.checkBox3)
        </th>
        <th></th>
    </tr>

    <tbody id="data-table">
        @foreach (var item in Model)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.name)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.checkBox1)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.checkBox2)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.checkBox3)
                </td>
                <td>
                    @Html.ActionLink("Edit", "Edit", new { id = item.Id }) |
                    @Html.ActionLink("Details", "Details", new { id = item.Id }) |
                    @Html.ActionLink("Delete", "Delete", new { id = item.Id })
                </td>
            </tr>
        }
    </tbody>



</table>
Jean Tehhe
  • 1,247
  • 5
  • 19
  • 38

1 Answers1

2

You can add a click handler to the submit button, and post to your action using Ajax, supplying it the URL and data. Add an ID to your button, and try the following.

$('#btnSubmit').click(function() {
   $.ajax({
       type: 'POST',
       url: '@Url.Action("Create", "YourControllerName")'
       // Not sure if you will have to supply the data since you have bound the controls 
       // in your action
   }); 
});
JB06
  • 1,881
  • 14
  • 28
  • Thanks Josh Its working but there is two issues 1.how to pass parameter from the var html variable like name and check box? 2.to make it work I remove the //[ValidateAntiForgeryToken] from the controller ,there is a way to not remove it?Thanks a lot Voted up! – Jean Tehhe May 29 '14 at 13:52
  • Glad its working. As for the html variable, you could parse the string for the variables, but it would be easier to just go back to how you had the view before. Can you post your full view code? As for the token, check this answer, should help you out - http://stackoverflow.com/questions/4074199/jquery-ajax-calls-and-the-html-antiforgerytoken – JB06 May 29 '14 at 14:06
  • Thanks Josh I've added the code please see my updated post,can you help with the parameters?Thanks a lot! – Jean Tehhe May 29 '14 at 14:29
  • Are your parameters being sent to the controller? Debug your action and see if the role object has any assigned properties and which ones are null. – JB06 May 29 '14 at 15:04