0

当我单击特定记录的编辑链接时,我想从视图剃刀中的控制器中检索该 ID 的数据,并以表格形式显示以进行编辑。但它抛出了一个错误。它正在从我的数据库中检索数据,但是一旦我点击视图,它就会在每个字段中引发错误:

错误:

编译器错误消息:CS1061:“System.Collections.Generic.IEnumerable”不包含“AppName”的定义,并且找不到接受“System.Collections.Generic.IEnumerable”类型的第一个参数的扩展方法“AppName”(您是否缺少 using 指令或程序集引用?

Source Error:
Line 8:      <fieldset>   
Line 9:      <div class="editor-label grid_2">
Line 10:         @Html.LabelFor(model => model.AppName) :
Line 11:     </div>
Line 12:     <div class="editor-field grid_3">

我不知道我做错了什么。这是我的代码:

单击编辑链接时调用的控制器:

  [Authorize]
        public ActionResult UpdateAPIForm(string id,string appname)
        {
            try
            {
                var a = HttpContext.User.Identity.Name;
                var context = new ndCorp_SiteEntities();

                var viewModel = (from du in context.DevUserInfoes
                                join dc in context.DevContactInfoes on du.UserName equals dc.UserName
                                join dm in context.DevMarketplaceInfoes on du.UserName equals dm.UserName
                                join dm1 in context.DevMarketplaceInfoes on dc.AppName equals dm1.AppName
                                where (du.UserName == a && dc.AppName == appname )
                                select new NewAPIFormModel()
                                    { 
                                        AppName = dc.AppName, 
                                        Email = dc.DevEmail ,
                                        OrgName = dc.OrgName ,
                                        DevName = dc.DevName ,
                                        ClientID = dc.ClientID ,
                                        ClientSecret = dc.ClientSecret ,
                                        RedUrl = dc.Url ,
                                        AppNameForMP = dm.NewAppNameForMP ,
                                        AppDesc = dm.AppDesc ,
                                        DoYouAgree = dm.DispInPublicMP.Value ,
                                        mobil = dm.ChkMobility.Value ,
                                        legal = dm.ChkLegal.Value ,
                                        docmgm = dm.ChkDocMgm.Value ,
                                        finser = dm.ChkFinSer.Value ,
                                        microoff = dm.ChkMicOff.Value  ,
                                        saleforce = dm.ChkSalesForce.Value ,
                                        scanner = dm.ChkScanMF.Value ,
                                        wordper = dm.ChkWordPer.Value ,
                                        deskext = dm.ChkDeskExt.Value ,
                                        other = dm.ChkOther.Value ,
                                        //updlogo = dm.UpdLogo,
                                       // pricing =Convert.ToString(dm.Pricing),
                                        pricingURL = dm.UrlPricing,
                                        contactinfo = dm.Contact,
                                    }).ToList();
                                //select dc;
                return View(viewModel);
                //return View();
            }
            catch (Exception ex)
            {
                Console.Write(ex);
                TempData["msg"] = ex.Message.ToString();
                return View();
            }
        }   

这是我的看法:

@model IEnumerable<N.Models.NewAPIFormModel>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)
    <fieldset>   
    <div class="editor-label grid_2">
        @Html.LabelFor(model => model.AppName) :
    </div>
    <div class="editor-field grid_3">
        @Html.TextBoxFor(model => model.AppName, new { @class = "demo-field-longer" })
    </div>
     <div class="editor-label1 grid_2">
        Must be unique
    </div>
    <div class="grid_3 error long" style="margin-left:250px">
    @Html.ValidationMessageFor(model => model.AppName)
    </div>


    <div class="clear"></div>
    <div class="editor-label grid_2">
        @Html.LabelFor(model => model.Email):
    </div>
    <div class="editor-field grid_3">
        @Html.TextBoxFor(model => model.Email, new { @class = "demo-field-longer" })
    </div>
    <div class="grid_3 error long" style="margin-left:250px">
    @Html.ValidationMessageFor(model => model.Email)
    </div>


      <div class="clear"></div>
    <div class="editor-label grid_2">
        @Html.LabelFor(model => model.DevName):
    </div>
    <div class="editor-field grid_3">
        @Html.TextBoxFor(model => model.DevName, new { @class = "demo-field-longer" })
    </div>
    <div class="grid_3 error long" style="margin-left:250px">
    @Html.ValidationMessageFor(model => model.DevName)
    </div>
    .
    .
    .
    .
 <div class="editor-field grid_2 submit">
        <input type="submit" value="Submit" id="demo-submit-button"/><br />
        @ViewData["DemoMessage"]
    </div>
    </fieldset>

这是我的模型:

 public class NewAPIFormModel
    {
        [Required]
        [Email]
        [DisplayName("Developer Email")]
        public string Email { get; set; }


        [Required]
        [DisplayName("Application Name")]
        public string AppName { get; set; }

        [Required]
        [DisplayName("Organization Name")]
        public string OrgName { get; set; }

        [Required]
        [DisplayName("Developer Name")]
        public string DevName { get; set; }

        [Required]
        [DisplayName("App Key/ClientID")]
        public string ClientID { get; set; }

        [Required]
        [DisplayName("Client_Secret")]
        public string ClientSecret { get; set; }

        [Required]
        [DisplayName("Redirect_url")]
        public string RedUrl { get; set; }

        [Required]
        [DisplayName("Application Name")]
        public string AppNameForMP { get; set; }

        [Required]
        [DisplayName("Application Description")]
        public string AppDesc { get; set; }
.
.
.
.
4

1 回答 1

0

您将模型定义为不包含属性的 NewAPIFormModels 集合AppName。实际上,其中的对象具有该属性。你需要有一个NewAPIFormModel作为你的模型。为此,您不应该使用 ToList()函数来实现您的查询。

您应该使用First()FirstOrDefault()根据您的需要使用。

var viewModel = (linq query here).First();
  • 如果您确定您的选择查询将始终返回比 go withFirst()更有意义的内容。

  • 否则,FirstOrDefault()当结果为空时继续处理。

这样,您可以获得单个NewAPIFormModel对象,而不是保留单个 NewAPIFormModel对象的集合。

在您的视图中定义您的强类型模型如下。

@model N.Models.NewAPIFormModel
于 2013-01-22T00:21:27.430 回答