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:
@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> </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> <button class='btn_grid remove-item edit-mode' id='0'>Cancel</button></td>");
$('#gridContent tbody:last').append(newRow);
}
$('.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();
}, "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();
});
});
</script
>
Controller:
#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.