gpt4 book ai didi

c# - 尝试在没有 Entity Framework 的情况下使用表单发布——我放弃了,但会保持打开状态,以防它帮助某人......在某个地方

转载 作者:行者123 更新时间:2023-11-29 22:31:59 26 4
gpt4 key购买 nike

在尝试让基本 CRUD 在我的 Web 应用程序中工作时,我遇到了一些问题。首先,这是我连接到数据库的方式:

public class DatabaseController : Controller
{
protected MySqlConnection conn;

public DatabaseController()
{
//Vul hier de juiste gegevens in!!
conn = new MySqlConnection("Server=localhost;Database=db;Uid=pw;Pwd=pw;");
}
}

这就是我最初加载行的方式:

 public Klant getAll(string naam)
{
MySqlTransaction trans = null;
Klant klanten = new Klant();

conn.Open();
trans = conn.BeginTransaction();
try
{
string selectQuery = @"select * from klant;";

MySqlCommand cmd = new MySqlCommand(selectQuery, conn);
//MySqlParameter naamParam = new MySqlParameter("@naam", MySqlDbType.VarChar);
//naamParam.Value = "%" + naam + "%";
//cmd.Parameters.Add(naamParam);
cmd.Prepare();


MySqlDataReader dataReader = cmd.ExecuteReader();
while (dataReader.Read())
{
int IdKlant = dataReader.GetInt32("idklant");
String Naam = dataReader.GetString("naam");
String Woonplaats = dataReader.GetString("woonplaats");
DateTime Geboortedatum = dataReader.GetDateTime("geboortedatum");
String Geslacht = dataReader.GetString("geslacht");
String Beschrijving = dataReader.GetString("beschrijving");


klanten.idklant = IdKlant;
klanten.naam = Naam;
klanten.woonplaats = Woonplaats;
klanten.geboortedatum = Geboortedatum;
klanten.geslacht = Geslacht;
klanten.beschrijving = Beschrijving;



}
}

catch (Exception e)
{
throw new Exception("Product niet toegevoegd: " + e);
}

finally
{
conn.Close();
}

return klanten;
}

这就是我在 View 中加载它们的方式:

@{ if (@Model == null)
{
<div>geen producten gevonden</div>
}
else
{
<div></div><br>
foreach (Klant kl in Model)
{

<tr>
<td>
@Html.DisplayFor(modelItem => kl.idklant)
</td>
<td>
@Html.DisplayFor(modelItem => kl.naam)
</td>

<td>
@Html.ActionLink("Details", "ShowKlant", new { id = kl.idklant }) |
@Html.ActionLink("Edit", "ShowKlant", new { id = kl.idklant }) |
@Html.ActionLink("Delete", "ShowKlant", new { id = kl.idklant })<br /><br />
</td>
</tr>

}
}
}

读取操作工作正常(在某种程度上)。

我能够从数据库加载所有行,每行都有一个详细信息/编辑/删除/选项,该选项使用每个相应行的正确 ID。

如果我点击具有 id3 的客户,则会加载此链接:

http://localhost:51596/Home/ShowKlant/3

即使在 actionResult 之上使用 [HttpPost] 重载,如下所示:

        public ActionResult ShowKlant(int? idklant)
{
Klant k = new Klant();
KlantDBController sc = new KlantDBController();
k = sc.getById(idklant);
return View(k);
}

[HttpPost]
public ActionResult ShowKlant(Klant klant)
{
return View(klant);
}

这些字段没有被填充。

running out of ideas

这是 View 在代码中的样子:

    @using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<fieldset>
<legend>Movie</legend>
<div class="editor-label">
@Html.LabelFor(model => model.idklant)
</div>
<div class="editor-label">
@Html.EditorFor(model => model.idklant)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.naam)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.naam)
@Html.ValidationMessageFor(model => model.naam)
</div>

<div class="editor-label">
@Html.LabelFor(model => model.woonplaats)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.woonplaats)
@Html.ValidationMessageFor(model => model.woonplaats)
</div>

<div class="editor-label">
@Html.LabelFor(model => model.geboortedatum)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.geboortedatum)
@Html.ValidationMessageFor(model => model.geboortedatum)
</div>

<div class="editor-label">
@Html.LabelFor(model => model.geslacht)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.geslacht)
@Html.ValidationMessageFor(model => model.geslacht)
</div>

<div class="editor-label">
@Html.LabelFor(model => model.beschrijving)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.beschrijving)
@Html.ValidationMessageFor(model => model.beschrijving)
</div>

<p>
<input type="submit" value="Save" />
</p>
</fieldset>
}

<div>
@Html.ActionLink("Back to List", "Index")
</div>

我可能遗漏了一些小东西......但我认为问题可能在于我如何加载详细信息页面的数据......我只是看不到它:/

我的 getById 代码本质上与 getAll 相同。

获取ID代码是这个

 string selectQuery = @"select * from klant where idklant = @idklant;";

这就是和我平常的SQL不同的地方。

如果我这样做:

 string selectQuery = @"select * from klant;";

它填充数据,但仅填充数据库中的最后一行,无论我使用哪个客户,它都会返回/填充最后一行。

一切:

enter image description here

不愉快的结果:

enter image description here

最佳答案

您的问题是 @idklant 从未设置过值。

您有一个名为 getById(int idklant) 的函数

MySqlCommand cmd = new MySqlCommand(selectQuery, conn); 
MySqlParameter idklantParam = new MySqlParameter("@idklant", MySqlDbType.Int32);
cmd.Parameters.Add(idklantParam);

并且您从未设置参数的值,仅设置类型。在 cmd.Parameters.Add 之前你应该有

idklantParam.value = idklant; // or whatever your parameter name is in your method

关于c# - 尝试在没有 Entity Framework 的情况下使用表单发布——我放弃了,但会保持打开状态,以防它帮助某人......在某个地方,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29721440/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com