0

While trying to get the basic CRUD to work in my Web application I have run into some problems. I have managed to use

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

to correctly query string wanted data to a new view.

Photo evidence:

enter image description here

Now I have scoured the internet for tutorials, and the method to display said query string data in a form, and i have found nothing at all that helped me.

this is the view code of the page that I wish to fill and my 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);
    }

Here is the view that 'pushes' the querystring:

    @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>

I would appreciate a nudge in the right direction, because i am completely confused at the moment.

I am preemptively assuming that my action results are completely wrong, however i am unsure how to un-wrong them.


more context:

    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 that loads my table:

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;
    }

This is what my initial view looks like:

enter image description here

And this is what i am trying to fill with said querystring:

enter image description here

7
  • I'm sorry but I really lost you after Photo evidence. What is it you are trying to do in the first place, - then I understand you've found some solution - but I don't know to what, and then what is the problem with the solution you've found ?!? Commented Apr 19, 2015 at 14:22
  • sorry, What i am trying to achieve is get the shown data from the querrystring into a form, with which i would be able do edit de db(this part i know how to do). But i cant get the data into the form. Commented Apr 19, 2015 at 14:25
  • still very fuzzy. Also these lines are very suspicisouly wrong to me : KlantDBController sc = new KlantDBController();k = sc.getById(idklant); (1) Why have you mentioned Entity Framework (2) Where is your data, what kind of DB (3) Did you create a edmx file (As in an EntityModel) for your data? (4) How or where that query string come from ? You seem to doing some basic things incorrectly and/or are on a very different plane of thinking. Have you looked at some basic asp-mvc examples? Commented Apr 19, 2015 at 14:31
  • those suspiciously wrong lines is what i assumed was wrong as well, i shall add my db Connnection and the SQL that loads my initial data. however this will make my question uncomfortably long. Commented Apr 19, 2015 at 14:43
  • If it's a controller you're instantiating inside an action method it's completely wrong and gives me the conclusion that you are really on the wrong highway. I'd suggest instead cleaning up your code completely, Do a Build->Clean from Visual Studio and zip the whole folder and put it up somewhere on google drive, dropbox and post a link here. Then update your question with (A) What it is you really want (B) What you've tried where narrow down where the problem is. You problem is probably not what you think it is, it is perhaps much more fundamental. : see tinyurl.com/stack-hints Commented Apr 19, 2015 at 14:48

1 Answer 1

2

I'll give you a dirty answer and point out a few things wrong in your question:

(1) : This page is originally generated to show a list of Klant. You've changed the code incorrectly to try and make it show one. You should generate a detail page instead.

@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)
//{

_"When you click details -> It shouldn't put in that query string. You should have a details method that shows a Klant details:

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.

This is 1/4 way there, you'll need something like this:

<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>
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.