如何在ASP.NET MVC中使用模型数据填充表单

时间:2015-04-19 14:02:10

标签: c# html asp.net-mvc-4 razor

在尝试让基本的CRUD在我的Web应用程序中工作时,我遇到了一些问题。我设法使用

Html.Beginform("bla","bla", FormMethod.Get) 

正确查询字符串所需数据到新视图。

照片证据:

enter image description here

现在我已经在互联网上搜索了教程,以及在表单中显示所述查询字符串数据的方法,而且我找不到任何帮助我的东西。

这是我要填写的页面的视图代码和我的ActionResults:

@using MvcApplication1.Models;
@model Klant
@{
    ViewBag.Title = "ShowKlant";
}

<h2>ShowKlant</h2>
<div id="producetop" class="row">
    @{ @*if (@Model == null)
            {
               <div>geen producten gevonden</div>
            }
            else
            {*@
    //foreach (Klant kl in Model)
    //{

    <div style="float: left">
        <fieldset id="produce">
            <form method="get">
                <div><labe>ID: </labe><input type="hidden" name="id" value="@Html.Value("","")" /></div>
                <div><label>Naam:</label> <input type="text" name="naam" value="" /></div>
                <div><label>Woonplaats:</label> <input type="text" name="woonplaats" value="" /></div>
                <div><label>Geboortedatum:</label> <input type="text" name="gebdat" value="" /></div>
                <div><label>Geslacht:</label> <input type="text" name="geslacht" value="" /></div>
                <div><label>Beschrijving:</label> <input type="text" name="beschrijving" value="" /></div>

            </form>
        </fieldset>
    </div>

        @*}*@
    }

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

Actionresults:

public ActionResult ShowKlant()
    {
        return View();
    }

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

以下是“推送”查询字符串的视图:

    @using MvcApplication1.Models;
@model List<Klant>
@{
    ViewBag.Title = "ShowAllKlanten";
}

<h2>ShowAllKlanten</h2>
@using (Html.BeginForm("ShowKlant", "Home", FormMethod.Get, new { @enctype = "multipart/form-data" }))
{
    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.DisplayFor(modelItem => kl.woonplaats)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => kl.geboortedatum)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => kl.geslacht)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => kl.beschrijving)
                </td>

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

我很感激在正确的方向上轻推,因为我现在完全感到困惑。

我先发制人地认为我的行动结果是完全错误的,但我不确定如何取消错误。


更多背景信息:

    public class DatabaseController : Controller
{
    protected MySqlConnection conn;

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

加载我的表的SQL:

public List<Klant> getAllKlanten(String naam)
    {
        MySqlTransaction trans = null;
        List<Klant> klanten = new List<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())
            {
                Klant klant = new Klant();
                klant.idklant = dataReader.GetInt32("idklant");
                klant.naam = dataReader.GetString("naam");
                klant.woonplaats = dataReader.GetString("woonplaats");
                klant.geboortedatum = dataReader.GetDateTime("geboortedatum");
                klant.geslacht = dataReader.GetString("geslacht");
                klant.beschrijving = dataReader.GetString("beschrijving");


                klanten.Add(klant);
                Console.Write(klant.naam);
            }
        }

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

        finally
        {
            conn.Close();
        }

        return klanten;
    }

这是我的初步观点:

enter image description here

这就是我想要填充所述查询字符串的内容:

enter image description here

1 个答案:

答案 0 :(得分:1)

我会给你一个的答案,并在你的问题中指出一些错误:

(1):最初生成此页面是为了显示Klant列表。您已经错误地更改了代码,尝试将其显示为一个。您应该生成详细信息页面。

@using MvcApplication1.Models;
@model Klant
@{
    ViewBag.Title = "ShowKlant";
}
<h2>ShowKlant</h2>
<div id="producetop" class="row">
@{ @*if (@Model == null)
    {
       <div>geen producten gevonden</div>
    }
    else
    {*@
//foreach (Klant kl in Model)
//{

_&#34;点击详细信息时 - &gt;它不应该放在该查询字符串中。您应该有一个显示Klant详细信息的详细信息方法:

public ActionResult ShowKlant()
{
    return View();
}

[HttpPost]
public ActionResult ShowKlant(int? idklant)
{
    Klant k = new Klant();
    KlantDBController sc = new KlantDBController();
    k = sc.getById(idklant);
    return View(k);
}
//I don't know which you want to use but it has to really be like this.
//
[HttpGet] //most likely should be a GET
public ActionResult ShowKlant(int? idklant)
{//you are ONLY receiving an idklant so you don't
 //need to shove more things into a query string...
    Klant k = new Klant();
    //get the data of your "Klant" and fill it here..
    k.naam = "Bob";
    //etc       
    return View(k); 
}
//the above method will expect a ShowKlant.cshtml
// that page should be generate as a "Details" page
//and should have @model Klant on the top.

这是1/4方式,你需要这样的东西:

<fieldset id="produce">
<form method="get">
<div><labe>ID: </labe><input type="hidden" name="id" value="@:model.naam" /></div>
<div><label>Naam:</label> <input type="text" name="naam" value="@model" /></div>