简体   繁体   English

如何在MVC C#中将名称链接到ID

[英]How can I link a name to an id in MVC c#

I'm new to MVC in addition to C#, so... please work with me. 我是C#之外的MVC新手,所以...请与我合作。

I'm developing a simple MVC project - 我正在开发一个简单的MVC项目-

tables - prospects and contacts 表-潜在客户和联系方式

My issue comes when I'm trying to link the prospect ID field in the contacts model to the prospect name from the prospect table. 当我尝试将联系人模型中的潜在客户ID字段链接到潜在客户表中的潜在客户名称时,就会出现我的问题。

Prospect Model - 前景模型-

public class Prospect
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Address { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string PostalCde { get; set; }
}

Activity Model - 活动模型-

public class Activity
{
    public int Id { get; set; }
    public string Type { get; set; }
    public DateTime Date { get; set; }

    public int ProspectId { get; set; }
    public int ProspectName{ get; set; }
}

Activity Controller 活动控制器

    public ActionResult Index()
    {
        return View(db.Activities.ToList());
    }

Activity Index View 活动索引视图

@foreach (var item in Model) {
<tr>
    <td>
        @Html.ActionLink(item.Type, "Edit", "Activity", new { id=item.Id }, null)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.Date)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.ProspectId)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.ProspectName)
    </td>
    <td>

        @Html.ActionLink("Delete", "Delete", new { id=item.Id })
    </td>
</tr>
}

How can I link the prospectname to the prospectid in the activity model. 如何在活动模型中将准客户名称链接到准客户。

Eventually, I'd like to essentially join an activity to a prospect always, so the id and name are both automatically generated, but for now, I'm fine writing in the prospect id. 最终,我希望从本质上始终将活动加入潜在客户,因此ID和名称都会自动生成,但是现在,我可以很好地写入潜在客户ID。

Any ideas would be greatly appreciated. 任何想法将不胜感激。

I am not sure I understand completely your requirement. 我不确定我是否完全理解您的要求。

In Edit when you trying to change the Activity instead if textbo, use the dropdown which showing all the available Prospect Ids so one can select from only available Ids. 在“编辑”中,当您尝试更改“活动”而不是文本框时,请使用下拉菜单显示所有可用的Prospect ID,以便只能从可用的ID中进行选择。 So if you have available Prospect Id you can easily get its Name from table. 因此,如果有可用的潜在客户ID,则可以轻松地从表中获取其名称。

Do you just need help with the LINQ statement? 您仅需要LINQ语句的帮助吗? If so, it would look something like this... 如果是这样,它将看起来像这样...

    Prospect p = dbContext.Prospects.Where(p => Name == "Smith").FirstOrDefault();

(...replacing "Smith" with whatever the name is, obviously). (...用明显的名字替换“ Smith”)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM