非静态方法需要目标。 [英] Non-static method requires a target.
本文介绍了非静态方法需要目标。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用Razor web表单和MVC使用C#。我想在表中添加新记录,但一旦点击保存但得到错误非静态方法需要目标。我不知道为什么会发生错误.FYI:这个表还没有任何数据。
浏览:
@ model ICollection < GP_WMS.Model.Models.JobNaming >
@using System.Linq;
@using System.Data.Entity;
@ {
ViewBag.Title =Job Naming;
}
@section featured {
< section class = 精选 >
< < span class =code-leadattribute> div class = content-wrapper >
< hgroup class = title > ;
< h1 > @ ViewBag.Title < / h1 >
< / hgroup >
< / div >
< / section >
}
< div style = width:100%; float:left; padding-bottom:50px; < span class =code-keyword>>
< h2 > 列出工作名称< / h2 >
< p > < / p >
< a href =' #' id = btnAddNew > @ Html.Raw(< ; img src <跨越ss =code-keyword> =' / images / file_add.png' < span class =code-attribute> title =' 添加新的 / > )< / a >
< a href =' #' onclick = check() id = btnSelectAll > @ Html.Raw(< img src =' / images / selectall.gif' title =' 全选' / > )< / a >
< a href =' #' onclick = 取消选中() id = btnDeselectAll > ; @ Html.Raw(< img src =' / images / deselectall.gif' title =' 取消全选' / > )< / a >
< a href =' #' id = btnDeleteAll > @ Html.Raw(< < span class =code-leadattribute> img src =' / images / delete all icon.png' title =' 全部删除 / > )< / a >
@ {var grid = new WebGrid(Model,canPage: true,rowsPerPage:50,selectionFieldName:selectedRow,ajaxUpdateContainerId:gridContent); grid.Pager(WebGridPagerModes.NextPrevious);
< div < span class =code-attribute> id = gridContent class = articleHeader article_lst >
@ grid.GetHtml(
htmlAttributes:new {id =GridID},
tableStyle:webGrid,
headerStyle:,
alternatingRowStyle:alt,
selectedRowStyle:select,
列:grid.Columns(
grid.Column(,style:col3,格式:@ < text > < input id = chkSelected < span class =code-attribute> class = display-mode name = 选择 type = 复选框 value = @ item.Value.JobNamingID / > < / text > ),
grid.Column(Customer,样式:col2,格式:@ < text > < span id = 客户 class = display-mode > @ item.CustomerId < / span >
@ Html.Label( Customer-Edit,(string)item.CustomerId,new {@class =edit-mode})< / text > ),
grid.Col umn(作业名称,样式:col2,格式:@ < text > < span id = JobName class = display-mode > @ item.JobName < / span >
@ Html.TextBox(JobName-Edit ,(字符串)item.JobNamingName,new {@class =edit-mode})< / text > ),
grid.Column(Is Active,样式: col3,格式:@ < text > @ {bool isActive = Convert.ToBoolean(item.IsActive);
< span < span class =code-attribute> id = IsActive class = display-mode > @ Convert.ToBoolean(item.IsActive)< / span >
@ Html.CheckBox(IsActive-Edit,isActive,new {@class =edit-mode})} < / text > ),
grid.Column(,style:col1,格式:@ < text > < div 样式 = padding-left:20px; >
< a href =' #' class = < span class =code-keyword> btn_grid edit-item display-mode id = @ item.JobNamingID > @ Html.Raw(< img src =' / images /edit-icon.png' title =' 编辑' / > )< / a >
< a href =' #' < span class =code-attribute> class = btn_grid display-mode delete-item id = @ item.JobNamingID > @Html .Raw(< img < span class =code-attribute> src =' / images / delete icon.png' title =' 删除' / > )< / a >
< a href =' #' 跨度> < span class =code-attribute> class = btn_grid save-item edit-mode id = @ item.JobNamingID > 保存< < span class =code-leadattribute> / a >
< a href =' #' class = btn_grid cancel-item edit-mode id = @ item.JobNamingID > 取消< < span class =code-leadattribute> / a >
< / div > < < span class =code-leadattribute> / text > )))
< / div >
}
< span class =code-keyword>< / 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。追加(< td style ='padding-left:20px'>< button class ='btn_grid add-item edit-mode'id ='0'> Add< / button>& nbsp;< buttonclass ='btn_grid remove-item edit-mode'id ='0'>取消< / 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-添加')。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(成功保存);
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(成功保存);
location.reload(); //刷新页面
});
});
< / script
>
< br $>
控制器:
#region插入&安培;保存
public JsonResult SaveJobNaming(JobNaming jobName)
{
使用(BulkMailing2015Context db = new BulkMailing2015Context(connection))
{
_jobNameRepository = new GenericRepository< JobNaming>(db);
var q = 来自 tbl in db.JobNamings
其中 tbl.JobNamingID == jobName.JobNamingID
选择 tbl;
if (q.Count()> 0 )< -------错误在这里
{
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
提前致谢。
解决方案
< blockquote>(function(){
('。edit-mode')。hide();
('#btnAddNew')。click (function(){
if(
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); // $(".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:
#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.
解决方案
(function () {
('.edit-mode').hide();
('#btnAddNew').click(function () { if (
这篇关于非静态方法需要目标。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文