开发者

can`t figure out error in my code, mixing jquery and razor

开发者 https://www.devze.com 2023-03-27 22:32 出处:网络
I am reading through a table in jquery and I am trying to pass the value to a method from a class.I am trying to write the following but can`t figure what is wrong.

I am reading through a table in jquery and I am trying to pass the value to a method from a class. I am trying to write the following but can`t figure what is wrong.

The error message is on the line if...:

MvcUI\Views\Shared\employee.cshtml(170,81): error CS1646: Keyword, identifier, or string expected after verbatim specifier: @ MvcUI\Views\Shared\employee.cshtml(170,83): error CS1056: Unexpected character '$' MvcUI\Views\Shared\employee.cshtml(170,90): error CS1056: Unexpected character '$' MvcUI\Views\Shared\employee.cshtml(170,103): error CS1012: Too many characters in character literal

Help please.

 if($.trim($(this).find('td:first').text())!='')
            {    
             @{       

                if (MvcUI.Employee.ContainsName(@:$.trim($(this).find('td:first').text())))
                {
                    @:$($(this).find('td:first').html($(this).find('td:first').text() + "<br />N开发者_如何转开发ame");
                } 
             }                  
            } 


There 2 way to do what you want:

  1. Preload employer names into some js array
  2. Use ajax to check if name is registred

I will describe the second way.

The JS code:

var tdText = jQuery.trim($(this).find('td:first').text());

if(tdText != '') {    
  jQuery.post
  (
     '/some_route/employers/checkname',
     {
        name = tdText
     },
     function(rsp) {
       if(rsp == 'true') {
         alert("name is registered"); //replace by your code
       }
       else
       {
         alert("name isn't registered"); //replace by your code
       }
     }
  );
}

The C# controller code:

[HttpPost]
public ActionResult CheckName(string name)
{
  return Json(MvcUI.Employee.ContainsName(name));
}
0

精彩评论

暂无评论...
验证码 换一张
取 消