Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am using Razor web form with MVC use C#. I want to add new record in table but once click save but got error "Non-static method requires a target". I don't know why that error happen.FYI: this table didn't have any data yet.

View:

HTML
@model ICollection<GP_WMS.Model.Models.JobNaming>
@using System.Linq;
@using System.Data.Entity;

@{
	ViewBag.Title = "Job Naming";
}

@section featured {
	<section class="featured">
		<div class="content-wrapper">
			<hgroup class="title">
				<h1>@ViewBag.Title</h1>
			</hgroup>
		</div>
	</section>
}



<div style="width:100%;float:left;padding-bottom:50px;">
	<h2>List 0f Job Name</h2>
	<p></p>
	<a href='#' id="btnAddNew">@Html.Raw("<img src='/images/file_add.png' title='Add New'/>")</a>
    <a href='#' onclick= "check()" id="btnSelectAll">@Html.Raw("<img src='/images/selectall.gif' title='Select All'/>")</a>
	<a href='#' onclick= "uncheck()"  id="btnDeselectAll">@Html.Raw("<img src='/images/deselectall.gif' title='Unselect All'/>")</a>
	<a href='#' id="btnDeleteAll">@Html.Raw("<img src='/images/delete all icon.png' title='Delete All'/>")</a>
	
    
    @{ var grid = new WebGrid(Model, canPage: true, rowsPerPage: 50, selectionFieldName: "selectedRow", ajaxUpdateContainerId: "gridContent"); grid.Pager(WebGridPagerModes.NextPrevious);
		<div id="gridContent" class="articleHeader article_lst">
			@grid.GetHtml(
            htmlAttributes: new { id = "GridID" },
            tableStyle: "webGrid",
			headerStyle: "",
			alternatingRowStyle: "alt",
			selectedRowStyle: "select",
			columns: grid.Columns(
            grid.Column("",style: "col3" , format: @<text><input id="chkSelected" class="display-mode" name="select" type="checkbox" value="@item.Value.JobNamingID" /></text>),
            grid.Column("Customer", style: "col2", format: @<text><span id="Customer" class="display-mode">@item.CustomerId</span>
						@Html.Label("Customer-Edit", (string)item.CustomerId, new { @class = "edit-mode" })</text>),
            grid.Column("Job Name", style: "col2", format: @<text><span id="JobName" class="display-mode">@item.JobName</span>
						@Html.TextBox("JobName-Edit", (string)item.JobNamingName, new { @class = "edit-mode" })</text>),
            grid.Column("Is Active", style: "col3", format: @<text>@{bool isActive = Convert.ToBoolean(item.IsActive);
						<span id="IsActive" class="display-mode">@Convert.ToBoolean(item.IsActive)</span>
						@Html.CheckBox("IsActive-Edit", isActive, new { @class = "edit-mode" })}</text>),
            grid.Column("", style: "col1", format: @<text><div style="padding-left:20px;">
			<a href='#' class="btn_grid edit-item display-mode" id="@item.JobNamingID">@Html.Raw("<img src='/images/edit-icon.png' title='Edit'/>")</a>
			<a href='#' class="btn_grid display-mode delete-item" id="@item.JobNamingID">@Html.Raw("<img src='/images/delete icon.png' title='Delete'/>")</a>
			<a href='#' class="btn_grid save-item edit-mode" id="@item.JobNamingID">Save</a>
			<a href='#' class="btn_grid cancel-item edit-mode" id="@item.JobNamingID">Cancel</a>
			</div></text>)))

		</div>
}
</div>


<script type="text/javascript">

    $(function () {

        $('.edit-mode').hide();

        $('#btnAddNew').click(function () {

            if ($('.add-item').length == 0) {

                var newRow = $("<tr>");
                newRow.append("<td>&nbsp;</td>");
                newRow.append("<td><select id = 'CustomerId-Add'><option value='1'>OCBC Bank</option><option value='2'>Ge-Pap</option><option value='3'>TESCO</option></select></td>");
                newRow.append("<td><input type='text' id='JobName-Add'/></td>");
                newRow.append("<td><input type='checkbox' id='IsActive-Add'/></td>");
                newRow.append("<td style='padding-left:20px'><button class='btn_grid add-item edit-mode' id='0'>Add</button>&nbsp;<button class='btn_grid remove-item edit-mode' id='0'>Cancel</button></td>");

                $('#gridContent tbody:last').append(newRow);
                //                $(".datepicker").datepicker({ format: 'dd/mm/yyyy', autoclose: true, todayBtn: 'linked' })
                //                $(".datepicker").datepicker("update", new Date());

                }



            $('.add-item').on('click', function () {
                var tr = $(this).parents('tr:first');
                var JobNamingID = $(this).prop('id');
                var CustomerId = tr.find('#CustomerId-Add').val();
                var JobName = tr.find('#JobName-Add').val();
                var IsActive = tr.find('#IsActive-Add').prop("checked");

                $.post(
						'/Master/SaveJobNaming/',
						{ JobNamingID: JobNamingID, CustomerId: CustomerId, JobName: JobName, IsActive: IsActive },
						function (item) {
						    tr.find('#CustomerId').text(item.CustomerId);
						    tr.find('#JobName').text(item.JobName);
						    tr.find('#IsActive').text(item.IsActive);
						    alert("Successfully Saved");
						    location.reload();      // refresh the page
						}, "json");
                tr.remove();
            });

            $('.remove-item').on('click', function () {
                var tr = $(this).parents('tr:first');
                tr.remove();
            });

        });

        $('.edit-item').on('click', function () {
            $('.edit-mode').hide();
            $('.delete-mode').hide();
            $('.display-mode').show();
            var tr = $(this).parents('tr:first');
            tr.find('.edit-mode, .display-mode').toggle();
        });



        $('.save-item').on('click', function () {
            var tr = $(this).parents('tr:first');
            var JobNamingID = $(this).prop('id');
            var CustomerName = tr.find('#CustomerName-Edit').val();
            var JobName = tr.find('#JobName-Edit').val();
            var IsActive = tr.find('#IsActive-Edit').prop("checked");
            $.post(
								'/Master/SaveJobNaming/',
								{ JobNamingID: JobNamingID, CustomerId: CustomerName, JobName: JobName, IsActive: IsActive },
								function (item) {
								    tr.find('#CustomerName').text(item.CustomerId);
								    tr.find('#JobName').text(item.JobName);
								    tr.find('#IsActive').text(item.IsActive);
								}, "json");
            tr.find('.edit-mode, .display-mode').toggle();
            alert("Successfully Saved");
            location.reload();      // refresh the page
        });



    });

    
    </script
>



Controller:

C#
#region Insert & Save
        public JsonResult SaveJobNaming(JobNaming jobName)
        {

            using (BulkMailing2015Context db = new BulkMailing2015Context(connection))
            {

                _jobNameRepository = new GenericRepository<JobNaming>(db);

                var q = from tbl in db.JobNamings
                        where tbl.JobNamingID == jobName.JobNamingID
                        select tbl;

                if (q.Count() > 0) <------- Error in here
                {
                    foreach (var item in q)
                    {
                        item.CustomerId = jobName.CustomerId;
                        item.JobName = jobName.JobName;
                        item.IsActive = jobName.IsActive;

                        _jobNameRepository.Update(item);
                    }
                }
                else
                {
                    _jobNameRepository.Insert(jobName);

                }

                _jobNameRepository.Save();

                return Json(new { JobNamingID = jobName.JobNamingID, CustomerId = jobName.CustomerId, JobName = jobName.JobName, IsActive = jobName.IsActive });
            }
        }
        #endregion





Thanks in advance.
Posted
Comments
F-ES Sitecore 26-Jan-16 5:14am    
You've just dumped about 300 lines of code....which line throws the error?

1 solution

Such fundamentals like static vs. non-static (instance) members are so important, that without perfect understanding of them one should not even dream about developing of ASP.NET, any UI applications or anything as advanced. You also should not just try to fix your problem. Instead, you should stop doing what you are doing and educate yourself. This stuff is fairly simple. Basically, my past answers explain it all:
C# windows base this key word related and its uses in the application[^],
What makes static methods accessible?[^],
Type casting in c#[^].

—SA
 
Share this answer
 
v4

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