Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
View

C#
@{
lang="xml">ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<fieldset>
    <br />
    <br />
    <br />

     <div align="center">

     @{

        @(Html.Telerik().Grid<SmartTrack.Web.DAL.SysUserList>()
        .DataBinding(dataBinding => dataBinding.Ajax().Select("Index", "User"))
        .Name("UserList")
        .DataKeys(keys => keys
            .Add(c => c.UserName)
            .RouteKey("UserName"))
        .Columns(columns =>
        {
            columns.Bound(o => o.UserName).Width(100);
            columns.Bound(o => o.FirstName).Width(200);
            columns.Bound(o => o.LastName).Width(250);
            columns.Bound(o => o.Active).ClientTemplate("<input type='checkbox' disabled='disabled' name='Active' <#=Active? checked='checked' : '' #> ;").Width(70).HtmlAttributes(new { style = "text-align:center" }); ;


        })
        .Pageable(pagerAction => pagerAction.PageSize(20))
        .Sortable()
        .Selectable()
        .Scrollable()
        .Groupable()
        .Filterable()
        .HtmlAttributes(new { style = "width:50%;" })
       )
         }
       </div>
       <br/>
       <div align="center">
        <table>
        <tr>
        <td>
            <button id="btnAdd" type="submit" style="height:40px;width:70px" ">Add</button>
            </td>
            <td>
            <button style="height:40px;width:70px" ">Edit</button>
            </td>
            <td>
            <button style="height:40px;width:70px" ">Delete</button>
            </td>
        </tr>
        </table>
       </div>
       </fieldset>

<script type="text/javascript">
    $(function () {
        $('#btnAdd').click(function () {
            $.ajax({
            type: "POST",
            data: $("form").serialize(),
            url: '@Url.Action("CreateUser","User")',
            success: function (result) {
                $('#cuscreate').html(result)
            }
            });
        });
    });
    </script>



This view contain a script from which the UserController Create method is call when the add button is clicked

Control

C#
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.Mvc; 
using SmartTrack.Web.Attributes; 
using SmartTrack.Web.Models; 
using SmartTrack.Web.DAL; 
using Telerik.Web.Mvc; 
namespace SmartTrack.Web.Controllers 
{ 
    public class UserController : Controller 
    { 
        SMARTTrackerEntities db = new SMARTTrackerEntities(); 
 
        // 
        // GET: /User/ 
        [GridAction] 
        public ActionResult Index() 
        { 
            //ViewData["UserGroup"] = DisplayContentEngine.getUserGroupList(); 
 
            return View(new GridModel(UserListEngine.getAllSystemUser())); 
        } 
 
        [HttpPost]  
        public ActionResult CreateUser() 
        { 
 
            ViewData["Location"] = DisplayContentEngine.getLocationList(); 
            ViewData["Branch"] = DisplayContentEngine.getBranchList(); 
            //var v = ViewData.Model = UserListEngine.getAllSystemUser(); 
            var user = new SysUserList(); 
 
            return View(user); 
        } 
 
        [HttpPost] 
        public ActionResult Create(SysUserList user) 
        { 
            try 
            { 
                // TODO: Add insert logic here 
                if (ModelState.IsValid) 
                { 
 
                    //Save Registration 
                    db.SysUserLists.AddObject(user); 
                    db.SaveChanges(); 
 
                    return RedirectToAction("Index"); 
                } 
                return View(user); 
            } 
            catch 
            { 
                return View(); 
            } 
 
        } 
 
        public ActionResult Edit(string username) 
        { 
            SysUserList sysuserlist = db.SysUserLists.Single(s => s.UserName == username); 
            return View(sysuserlist); 
 
        } 
    } 
} 


Create View

C#
@model SmartTrack.Web.DAL.SysUserList 
 
@{ 
    ViewBag.Title = "CreateUser"; 
    Layout = "~/Views/Shared/_Layout.cshtml"; 
} 
 
<h2>Create</h2> 
 
 
@using (Html.BeginForm()) 
{ 
    <div id="cuscreate"> 
        <fieldset> 
 
        <table> 
            <tr> 
                <td> 
                    <label>First Name</label> 
                </td> 
                <td> 
                    @Html.TextBoxFor(model => model.FirstName) 
                </td> 
                <td> 
                    <label>Last Name</label> 
                </td> 
                <td> 
                    @Html.TextBoxFor(model => model.LastName) 
                </td> 
            </tr> 
            <tr> 
                <td> 
                    <label>User Name</label> 
                </td> 
                <td> 
                    @Html.TextBoxFor(model => model.UserName) 
                </td> 
                <td> 
                    <label>Password</label> 
                </td> 
                <td> 
                    @Html.PasswordFor(model => model.userPwd) 
                </td> 
            </tr> 
            <tr></tr> 
            <tr> 
                <td> 
                    <label>Location</label> 
                </td> 
                <td> 
                    @(Html.Telerik().DropDownList() 
                    .Name("ddLocation") 
                    .BindTo((IEnumerable<DropDownItem>)ViewData["Location"]) 
                    .CascadeTo("ddlBranch") 
                    ) 
                </td> 
                <td> 
                     <label>Branch</label> 
                </td> 
                <td> 
                    @(Html.Telerik().DropDownList() 
                    .Name("ddlBranch") 
                    .BindTo((IEnumerable<DropDownItem>)ViewData["Branch"]) 
                    ) 
                </td> 
            </tr> 
 
        </table> 
 
 
        </fieldset> 
    </div> 
}


When the add button is click nothing happen can someone tell my whats my issue?

Thanks in advance

Regards Kurt
Posted
Updated 10-Jul-12 15:40pm
v2
Comments
Sergey Alexandrovich Kryukov 10-Jul-12 23:33pm    
Where this button is in code? Where do you add an event handler ("+=")?
--SA
Trak4Net 11-Jul-12 0:25am    
I have not been able to invest as much into learning MVC3 or jQuery so if I am way off I apologize. In your script for the index view you are referencing this $('#cuscreate') which doesn't exist in the index view. Maybe I am mistaken but doesn't that html element need to exist for jQuery to access it and push the results into that element?

1 solution

Please modify the signature of CreateUser action to

C#
[HttpPost]  
public ActionResult CreateUser(FormCollection collection) 
{ 
   // collection will have the data received from View. you can process it.
}


Let me know if it doesn't work.

All the best,
Kiran
 
Share this answer
 
Comments
Allikurt 11-Jul-12 9:42am    
It does not work it still does nothing.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900