当我点击特定记录的编辑链接时,我想从视图剃刀中的控制器检索该ID的数据,并以表格形式显示以进行编辑。但这是一个错误。它从我的数据库中检索数据,但是一旦我点击视图就会在每个字段中抛出错误:
错误:
编译器错误消息:CS1061:'System.Collections.Generic.IEnumerable'不包含'AppName'的定义,并且没有扩展方法'AppName'接受类型'System.Collections.Generic.IEnumerable'的第一个参数可以找到(你是否错过了使用指令或汇编参考?
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; }
.
.
.
.
答案 0 :(得分:0)
您将模型定义为NewAPIFormModel的集合,该集合不包含AppName
属性。实际上,它内部的对象具有该属性。您需要使用单个NewAPIFormModel
作为模型。为此,您不应使用ToList()
函数来实现查询。
根据您的需要,您应该使用First()
或FirstOrDefault()
。
var viewModel = (linq query here).First();
如果你确定你的选择查询总会返回一些内容,而不是First()
,这更有意义。
否则请使用FirstOrDefault()
并在结果为空时处理该案例。
这样您就可以获得单个NewAPIFormModel
对象而不是保留单个NewAPIFormModel
对象的集合。
在您的视图中,将您的强类型模型定义如下。
@model N.Models.NewAPIFormModel