如何将一对多从控制器传递到mvc4中的视图

2023-12-12

好的,这将是在你们帮助我进行更改之后,我假设我在某处遇到语法错误

View

@model OilNGasWeb.ModelData.Clients

@{
ViewBag.Title = "Index";
}


<h2>County's for </h2> 

<p>
@Html.ActionLink("Create New", "Create",new { id = Model.ClientID },null) 
</p>


<table>
<tr>

    <th>
        @Html.DisplayNameFor(model => model.County) 
    </th>

    <th>
        @Html.DisplayNameFor(model => model.Note) 
    </th>

    <th>
        @Html.DisplayNameFor(model => model.Comment) 
    </th>

</tr>

@foreach (var item in Model.Countys) {
<tr>

    <td>
        @Html.DisplayFor(modelItem => item.County)
    </td>

    <td>
        @Html.DisplayFor(modelItem => item.Note)
    </td>

    <td>
        @Html.DisplayFor(modelItem => item.Comment)
    </td>

    <td>
        @Html.ActionLink("Edit", "Edit", new { id=item.CountyID }) 
        @Html.ActionLink("Details", "Details", new { id=item.CountyID }) 
        @Html.ActionLink("Delete", "Delete", new { id=item.CountyID })
    </td>

</tr>
}

</table>

模范客户

 [Table("Clients")]
public class Clients
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]

    public int ClientID { get; set; }

    public string Client { get; set; }
    public string Address { get; set; }
    public string State { get; set; }
    public string City { get; set; }
    public string County { get; set; }
    public int Zip { get; set; }
    public string Phone { get; set; }
    public string LogoLocation { get; set; }
    public string ContactName { get; set; }
    public string ContactPhone { get; set; }
    public string ContactEmail { get; set; }
    public int Authorized { get; set; }

    public string Note { get; set; }
    public string Comment { get; set; }

    public virtual ICollection<Countys> Countys { get; set; }

}

模范县

 [Table("Countys")]
public class Countys
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]

    public int CountyID { get; set; }
    public int ClientID { get; set; }

    public string County { get; set; }
    public string Note { get; set; }
    public string Comment { get; set; }

    public virtual ICollection<TownShips> Townships { get; set; }

}

县长

public ActionResult Index(int id)
{
var cnty = from r in db.Clients
where r.ClientID == id
select r;
if (cnty != null)
{
return View(cnty); // View returns an error here
}
return HttpNotFound();

视图返回错误,但我无法进入它......找出它是什么......想法?


出于可扩展性的原因,您应该创建不属于域模型一部分的 ViewModel,并将它们传递到几乎所有视图中。

查看型号:

public class IndexViewModel
{
  public int ClientID { get; set; }
  public IEnumerable<Clients> Clients { get; set; }
}

查看(.cshtml):

@model OilNGasWeb.Models.Home.IndexViewModel

@{
  ViewBag.Title = "Index";
}


<h2>County's for </h2> 

<p>
  // send a ClientID with this action link
  @Html.ActionLink("Create New", "Create", new { clientid = Model.ClientID } ) 
</p>

//... etc

我还建议实际拼写出您的变量。所有这些都会被编译下来,因此通常最好编写可维护的代码而不是短处理变量。

控制器

public ActionResult Index(int id)
{
  //Lambda (just for an example, there is nothing wrong with LINQ expressions)
  var client = db.Clients
    .FirstOrDefault(c => c.ClientID == id);

  if (client != null)
  {
    var model = new IndexViewModel();
    model.ClientID = id;
    model.Clients = // some value I don't understand

    // My preference/opinion (programming religion) is to prefix with this
    // so others know if the method is *this* class, *base* class etc
    return this.View(model); 
  }

  return HttpNotFound();
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何将一对多从控制器传递到mvc4中的视图 的相关文章

  • Unity3D StartCoroutine 调用一个函数,该函数什么时候返回?

    我知道Unity3D StartCoroutine调用了一个与StartCoroutine在同一线程上运行的函数 但是被调用的函数什么时候返回到原始调用者 我在互联网上查找了一个很好的 Unity3D Coroutine 示例 但找不到完整
  • 以相反的顺序迭代可变参数模板参数

    如果我手动反转传递给它的模板参数的顺序 以下代码将起作用 template
  • DispatcherTimer 未按时执行

    我正在使用 c 中的 DispatchTimer 编写一个时钟应用程序 但由于某些原因 我的时钟似乎时不时地跳过 1 秒 例如 52 秒 gt 54 秒 跳过 53 秒 在我看来 计时器并不是每秒都执行一次 DispatcherTimer
  • C 中的变量定义是什么意思[重复]

    这个问题在这里已经有答案了 你们能告诉我 这在 C 中意味着什么吗 define Privileged Data Privileged Data static int dVariable 编译器对变量进行寻址有特殊意义吗 这只是一个宏Pri
  • C修改printf()输出到文件

    有没有办法修改printf为了将字符串输出到文件而不是控制台 我尝试在互联网上查找一些内容 发现了类似的电话dup dup2 and fflush这可能与此有关 EDIT 也许我不清楚 问题是这是C考试问题 问题如下 解释一个通常将字符串输
  • 为什么opencv videowriter这么慢?

    你好 stackoverflow 社区 我有一个棘手的问题 我需要你的帮助来了解这里发生了什么 我的程序从视频采集卡 Blackmagic 捕获帧 到目前为止 它工作得很好 同时我用 opencv cv imshow 显示捕获的图像 它也工
  • 通过单个 GPIO 引脚转储闪存

    我正在使用 Infineon 的 XMC4500 Relax Kit 并尝试通过单个 GPIO 引脚提取固件 我非常天真的想法是通过 GPIO 引脚一次转储一位 然后用逻辑分析仪以某种方式 嗅探 数据 伪代码 while word by w
  • 通过引用传递时取消引用指针

    当通过引用传递给函数时取消引用指针时会发生什么 这是一个简单的例子 int returnSame int example return example int main int inum 3 int pinum inum std cout
  • 为什么假设 send 可能返回的数据少于在阻塞套接字上传输的请求数据?

    在流套接字上发送数据的标准方法始终是调用 send 并写入一大块数据 检查返回值以查看是否发送了所有数据 然后再次调用 send 直到整个消息被接受 例如 这是一个常见方案的简单示例 int send all int sock unsign
  • 将成员函数作为参数传递/c++

    我想用 C 实现一个类b可以通过封装该迭代器类型的成员集进行某种迭代 喜欢 b object for each x do function f so 函数 f会得到每个人的x成员并做任何事情 比方说 void function f x me
  • 特定设备的不同字体大小

    我目前正在开发通用应用程序 我需要分别处理移动设备和桌面的文本框字体大小 我找到了一些方法 但都不能解决问题 使用 VisualStateManager 和 StateTrigger 为例
  • 指示泛型返回动态类型的对象

    这个问题是我原来问题的后续问题here https stackoverflow com questions 2541184 using a type object to create a generic 假设我有以下泛型类 简化 class
  • 使用scanf()时如何区分整数和字符

    我只是使用该功能scanf 代码如下 scanf d a printf d a 当我输入1时 它会像我想要的那样打印1 但即使我输入 1a 它也会像以前一样打印 1 当用户输入非整数时 例如 2 3 12ab 1 a 我想向用户显示 输入整
  • 线程安全的 C++ 堆栈

    我是 C 新手 正在编写一个多线程应用程序 不同的编写者将对象推入堆栈 读者将它们从堆栈中拉出 或至少将指针推入对象 C 中是否有任何内置结构可以在不添加锁定代码等的情况下处理此问题 如果没有 那么 Boost 库呢 EDIT 你好 感谢您
  • 如何在 C# 中使用 XmlDsigC14NTransform 类

    我正在尝试使用规范化 xml 节点System Security Cryptography Xml XMLDsigC14nTransformC net Framework 2 0 的类 该实例需要三种不同的输入类型 NodeList Str
  • C# 中处理 SQL 死锁的模式?

    我正在用 C 编写一个访问 SQL Server 2005 数据库的应用程序 该应用程序是数据库密集型的 即使我尝试优化所有访问 设置适当的索引等 我预计迟早会遇到死锁 我知道为什么会发生数据库死锁 但我怀疑我能否在某个时候发布不发生死锁的
  • 为什么WCF中不允许方法重载?

    假设这是一个ServiceContract ServiceContract public interface MyService OperationContract int Sum int x int y OperationContract
  • C++ 标准中短语“构造函数没有名称”的含义

    在尝试理解 C 标准中的 构造函数没有名称 这句话时 我似乎在 clang 中发现了一个错误 有人可以证实这一点吗 VS2015 and gcc rejects this code and I think they it are is co
  • 如何强制执行特定的 UserControl 设计

    我正在编写一个基本用户控件 它将由一堆其他用户控件继承 我需要对所有这些后代控件强制执行某种设计 例如 顶部必须有几个按钮以及一个或两个标签 后代用户控件区域的其余部分可以自由放置任何内容 最初 我认为我可以将一个面板放到 Base Use
  • 将文本从文本文件添加到 PDF 文件[重复]

    这个问题在这里已经有答案了 这是我的代码 using FileStream msReport new FileStream pdfPath FileMode Create step 1 using Document pdfDoc new D

随机推荐