我该如何解决这个错误 [英] How can I solve this error

查看:76
本文介绍了我该如何解决这个错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  public  ActionResult Register(Register regs1)

方法我试图从Register类发布数据。但它显示错误:

List< register>不包含UserId的定义



添加的代码块 - OriginalGriff [/ edit]



我尝试过:



 使用系统; 
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Web;
使用 System.Web.Mvc;
使用 AJaxJson.EDMX;
使用 AJaxJson.Models;

命名空间 AJaxJson.Controllers
{
public class SampleController:Controller
{
Database1Entities4 db = new Database1Entities4();
// GET:示例
public ActionResult Index()
{
ViewBag.country = new SelectList(db.Countries, CountryId 国家或地区名称);
return View();
}
[HttpGet]
public JsonResult CategoryList( int ? id)
{
var resultlist = db.States.Where(m = > m.CountryId == id)。ToList();
// var res = from i in db.States其中i.CountryId == id select i;
var result = new 列表< models.state>(来自 i 结果列表
选择 new Models.State
{
StateId = i.StateId,
StateName = i.StateName,
CountryId = Convert.ToInt32( i.CountryId)
});



// var p = db中的n。 n.CountryId == id的状态选择n;
return Json(结果,JsonRequestBehavior.AllowGet);
}

// [HttpGet]
// public JsonResult GetStatesList(int id)
// {
// var States = from a db.States,其中a.CountryId == id选择a;
// 返回Json(States,JsonRequestBehavior.AllowGet);
// }
[HttpPost]
public ActionResult Register(Register regs1)
{
List< register> k = new 列表< register>()
{
UserId = regs1.UserId,
UserName = regs1.UserName,
MobileNo = regs1.MobileNo,
StateId = regs1.StateId,
CountryId = regs1.CountryId

};
db.Registers.Add(k);
db.SaveChanges();

return 查看();
}
}
}



-------------------- -----------------------------

查看: -



 @ {
ViewBag.Title =Index;
}
< h2 > 索引< / h2 > ;
< script src = 〜/ Content / jquery -1.8.2.min.js > < / script >

< span class =code-keyword>< div >
< div >
userid:@Ht ml.TextBoxFor(m => m.UserId,new {@id =txtname})< br / >
uname:@ Html.TextBoxFor (m => m.UserName,new {@id =txtfname})< br / >
mobil :@ Html.TextBoxFor(m => m.MobileNo,new {@id =txtlname})< br / >

< / div >
@ Html.DropDownList(Country,ViewBag.country as SelectList, - select - )
@ Html.DropDownList(State,new SelectList(string.Empty),--- select ---)
< 输入 id = Submit1 type = 提交 value = submit / >
< / div >
< script 类型 = text / javascript language = javascript >
$(' #Country')。change( function (){
// alert(hi this is sekar
var varCountryid = $(< span class =code-string>' #Country')。val();
var varCountryname = $(' Country' )文本();

// alert(shdhhh;
var varprocessmsg = < option> ....请稍候....< / option>
$(' #State')。html(varprocessmsg).show();
// var Url =〜/ ajax / getstate?id=+ varCountryid;
// alert(Url);

$ .ajax({
url: / Sample / CategoryList?id = + varCountryid,
type: GET
dataType: json
成功: function (data){
var markup = < option value ='0'>选择状态< / option>;
// alert('sucessssssssss');
for var i = 0 ; i< data.length ; i ++){

markup + = < option value = + data [i] .StateId + > + data [i] .StateName + < / option>;
}

// alert('after for loop');
$( #State)。html(markup).show() ;

},

});


});



#sub .click ( function (){
alert(' 点击');
var cid = $(' #Country')。val();
var sid = $(' #State')。val();
var userid = $(' #UserId')。val();
var username = $(' #UserName')。val( );
var MobileNo = $(' #MobileNo')。val();
alert(la stname);
var mobilenumber = $(' #txtnumber )VAL();
var Countrymodel = {
Countryid:cid,Stateid:sid,userid:userid,firstname:firstname,lastname:lastname,phonenumber:mobilenumber

};


alert(cid);
$ .ajax({
url:' / ajax / register'
类型:' POST'
data:Countrymodel,
contenttype:' application / json:charset = utf-8'
成功: function (data){
alert(data)
}
});
});
< / script >

解决方案

' #Country')。change( function (){
< span class =code-comment> // alert(hi this is sekar
var varCountryid =


' #Country')。val();
var varCountryname =


' Country')。text();

// alert(shdhhh;
var varprocessmsg = < span class =code-string> < option> ....请等待....< / option>

In

public ActionResult Register(Register regs1)

Method i tried to post data from Register class .But it is showing error :
List<register> does not contain a definition for UserId

[edit]Code blocks added - OriginalGriff[/edit]

What I have tried:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AJaxJson.EDMX;
using AJaxJson.Models;

namespace AJaxJson.Controllers
{
    public class SampleController : Controller
    {
        Database1Entities4 db = new Database1Entities4();
        // GET: Sample
        public ActionResult Index()
        {
            ViewBag.country = new SelectList(db.Countries, "CountryId", "CountryName");
            return View();
        }
        [HttpGet]
        public JsonResult CategoryList(int? id)
        {
            var resultlist = db.States.Where(m => m.CountryId == id).ToList();
            //var res = from i in db.States where i.CountryId == id select i;
            var result = new List<models.state>(from i in resultlist
                                                select new Models.State
                                                {
                                                    StateId = i.StateId,
                                                    StateName = i.StateName,
                                                    CountryId = Convert.ToInt32(i.CountryId)
                                                });



            //var p = from n in db.States where n.CountryId == id select n;
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        //[HttpGet]
        //public JsonResult GetStatesList(int id)
        //{
        //    var States = from a in db.States where a.CountryId == id select a;
        //    return Json(States, JsonRequestBehavior.AllowGet);
        //}
        [HttpPost]
        public ActionResult Register(Register regs1)
        {
            List<register> k = new List<register>()
            {
                UserId = regs1.UserId,
                UserName = regs1.UserName,
                MobileNo = regs1.MobileNo,
                StateId = regs1.StateId,
                CountryId = regs1.CountryId

            };
            db.Registers.Add(k);
            db.SaveChanges();

            return View();
        }
    }
}


-------------------------------------------------
view:-

@{
    ViewBag.Title = "Index";
}
<h2>Index</h2>
<script src="~/Content/jquery-1.8.2.min.js"></script>

<div>
    <div>
        userid: @Html.TextBoxFor(m => m.UserId, new { @id = "txtname" })<br />
        uname:  @Html.TextBoxFor(m => m.UserName, new { @id = "txtfname" })<br />
        mobil: @Html.TextBoxFor(m => m.MobileNo, new { @id = "txtlname" })<br />

    </div>
    @Html.DropDownList("Country", ViewBag.country as SelectList, "--select--")
    @Html.DropDownList("State", new SelectList(string.Empty), "---select---")
    <input id="Submit1" type="submit" value="submit" />
</div>
<script type="text/javascript" language="javascript">
    $('#Country').change(function () {
       // alert("hi this is sekar"
        var varCountryid = $('#Country').val();
        var varCountryname = $('Country').text();

       // alert("shdhhh";
        var varprocessmsg = "<option>....please wait....</option>"
        $('#State').html(varprocessmsg).show();
        // var Url = "~/ajax/Getstate?id" = "+varCountryid";
        // alert(Url);

        $.ajax({
            url:"/Sample/CategoryList?id=" + varCountryid,
            type:"GET",
            dataType:"json",
            success:function (data) {
                var markup = "<option value='0'>select states</option>";
               // alert('sucessssssssss');
                for (var i = 0; i < data.length;i++) {

                    markup += "<option value=" + data[i].StateId + ">" + data[i].StateName + "</option>";
                }

               //alert('after for loop');
                $("#State").html(markup).show();

            },

        });


    });



    "#sub".click(function(){
        alert('click');
        var cid = $('#Country').val();
        var sid = $('#State').val();
        var userid = $('#UserId').val();
        var username = $('#UserName').val();
        var MobileNo = $('#MobileNo').val();
        alert(lastname);
        var mobilenumber = $('#txtnumber').val();
        var Countrymodel = {
            Countryid: cid, Stateid: sid, userid: userid, firstname: firstname, lastname: lastname, phonenumber: mobilenumber

        };


    alert(cid);
    $.ajax({
        url: '/ajax/register',
        type: 'POST',
        data: Countrymodel,
        contenttype: 'application/json:charset=utf-8',
        success: function (data) {
            alert(data)
        }
    });
    });
</script>

解决方案

('#Country').change(function () { // alert("hi this is sekar" var varCountryid =


('#Country').val(); var varCountryname =


('Country').text(); // alert("shdhhh"; var varprocessmsg = "<option>....please wait....</option>"


这篇关于我该如何解决这个错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆