我正在使用 EF 制作一个应用程序来进行简单的域名管理。它包含数据库中的客户端、域和 TLD 类型。
我创建了 Controller ,允许我在我的数据库中分别创建客户端、域和 TLD。我想要实现的是直接在客户页面下添加域的可能性,以避免每次创建新域名时都选择 ClientID,并尽量减少在数据库中创建新域名时要经过的页面数量。
到目前为止,这是我正在使用的模型:
Klient (client model) :
public class Klient
{
public int KlientID { get; set; }
public string Imie { get; set; }
public string Nazwisko { get; set; }
public string Firma { get; set; }
public virtual ICollection<Domena> Domeny { get; set; }
}
Domena(领域模型):
public class Domena
{
public int DomenaID { get; set; }
public int TLDID { get; set; }
public int KlientID { get; set; }
public string Nazwa { get; set; }
public virtual TLD TLD { get; set; }
public virtual Klient Klient { get; set; }
}
TLD(顶级域名模型):
public class TLD
{
public int TLDID { get; set; }
public string Typ { get; set; }
public int Cena { get; set; }
public virtual ICollection<Domena> Domeny { get; set; }
}
允许我创建新域名的 Controller 方法是:
// GET: /Domena/Create
public ActionResult Create()
{
ViewBag.TLDID = new SelectList(db.TLDs, "TLDID", "Typ");
ViewBag.KlientID = new SelectList(db.Klienci, "KlientID", "Firma");
return View();
}
//
// POST: /Domena/Create
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Domena domena)
{
if (ModelState.IsValid)
{
db.Domeny.Add(domena);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.TLDID = new SelectList(db.TLDs, "TLDID", "Typ", domena.TLDID);
ViewBag.KlientID = new SelectList(db.Klienci, "KlientID", "Imie", domena.KlientID);
return View(domena);
}
这就是创建 View 的字段集:
<fieldset>
<legend>Domena</legend>
<div class="editor-label">
@Html.LabelFor(model => model.TLDID, "TLD")
</div>
<div class="editor-field">
@Html.DropDownList("TLDID", String.Empty)
@Html.ValidationMessageFor(model => model.TLDID)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.KlientID, "Klient")
</div>
<div class="editor-field">
@Html.DropDownList("KlientID", String.Empty)
@Html.ValidationMessageFor(model => model.KlientID)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Nazwa)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Nazwa)
@Html.ValidationMessageFor(model => model.Nazwa)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
是的,我想做的是在 DomenaController(上面的 Create 方法所在的位置)下创建一个名为 Add_Domain 的新方法,并为该 Add_Domain 方法创建一个新的 View,这将允许我在当前下创建一个新的域名查看客户端。所以我想运行该应用程序并转到/Clients/Index 单击客户端上的详细信息,假设 ID = 6,所以我现在正在查看 Clients/Details/6。在此页面上,我添加了以下操作链接:
@Html.ActionLink("Add Domain", "Add_Domain", "Domena", new { id = Model.KlientID })
这给了我奇怪的链接: /Klient/Add_Domain?Length=6 代替 : /域/Add_Domain/6 但是,当我删除“new {id = Model.KlientID }”位时,它会创建正常链接......
无论如何,我如何修改 GET 和 POST httpPost,这样我就不必从列表中选择客户端,并且域名将添加到我单击 Add_Domain 方法的客户端?
编辑: 路由配置
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
编辑 2:
链接问题现已修复,非常感谢。问题的另一部分仍然存在:上面描述的新 Add_Domain 方法应该是什么样子(GET 和 POST)? - 它应该采用 KlientID 的值,而不是显示可供选择的客户端列表。
最佳答案
您的操作链接使用了错误的签名:
public static MvcHtmlString ActionLink(
this HtmlHelper htmlHelper,
string linkText, // "Add Domain"
string actionName, // "Add_Domain"
Object routeValues, // "Domena"
Object htmlAttributes // new { id = Model.KlientID }
)
你应该使用 this one :
public static MvcHtmlString ActionLink(
this HtmlHelper htmlHelper,
string linkText,
string actionName,
string controllerName,
Object routeValues,
Object htmlAttributes
)
例如:
@Html.ActionLink(
"Add Domain",
"Add_Domain",
"Domena",
new { id = Model.KlientID },
null)
这应该会为您提供预期的输出。
关于c# - 试图让 View 记住 ClientID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15999195/