w.c.nm
w.c.nm

Reputation: 61

How mvc model how which table to store

I'm create an mvc website to save the data to the database, but its can not save the data to the database.

Here is my web form

@using (Html.BeginForm())
        {
            @Html.ValidationSummary(true)

            <fieldset>

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



                </div>

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

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

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

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

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

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

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

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

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

                <p>
                    <input type="submit" value="res" id="b1" disabled onclick="confirm('Sure?')" />
                </p>
            </fieldset>

Here is my controller

[HttpPost]
    public ActionResult reservehotel(Reserve p)
    {
        if (ModelState.IsValid)
        {
            using (_db)
            {
                _db.hinfo.Add(p);
                _db.SaveChanges();
                ModelState.Clear();
                p = null;
                return RedirectToAction("Index", "Home");
            }
        }

        return View(p);
    }

The controller have run the Redirect

Here is my modal

public class Reserve
{
    [Key]
    public int resID { get; set; }
    [Required]
    public string tname { get; set; }
    [Required]
    public string phone { get; set; }
    [Required]
    public string icno { get; set; }
    [Required]
    public string gender { get; set; }
    [Required]
    public string cate { get; set; }
    [Required]
    public string hname { get; set; }
    [Required]
    public string htype { get; set; }
    [Required]
    public string hfrom { get; set; }
    [Required]
    public string hto { get; set; }
    [Required]
    public string hcost { get; set; }

}

Here is my db

public class DatabaseContext : DbContext
    {
        public DatabaseContext()
            : base("DefaultConnection")
        {

        }
        public DbSet<Reserve> hinfo { get; set; }

    }

Here is my Table

 **CREATE TABLE [dbo].[hinfo] (
    [Id]     INT           NOT NULL,
    [tname]  NVARCHAR (50) NOT NULL,
    [phone]  NVARCHAR (50) NOT NULL,
    [icno]   NVARCHAR (50) NOT NULL,
    [gender] NVARCHAR (50) NOT NULL,
    [cate]   NVARCHAR (50) NOT NULL,
    [hname]  NVARCHAR (50) NOT NULL,
    [htype]  NVARCHAR (50) NOT NULL,
    [hfrom]  NVARCHAR (50) NOT NULL,
    [hto]    NVARCHAR (50) NOT NULL,
    [hcost]  NVARCHAR (50) NOT NULL,
    PRIMARY KEY CLUSTERED ([Id] ASC)
);**

I have some question to ask

1) Why I can not store the data to the database but I can read the data from database?

2) Is any miss taken in my code?

3) How the modals know which table to store?

Upvotes: 1

Views: 104

Answers (2)

Felipe Cruz
Felipe Cruz

Reputation: 1149

Regarding your third question, the following line:

public DbSet<Reserve> hinfo { get; set; }

creates a DbSet property for the Reserve entity. In Entity Framework terminology, an entity set typically corresponds to a database table, and an entity corresponds to a row in the table.

Upvotes: 1

Zohaib Nauman
Zohaib Nauman

Reputation: 3

Reserve is you db table? if it is your table then you have to get data to store in it in controller like in custom php we get data $_POST[name]

Upvotes: 0

Related Questions