在数据库中获取数据而不在asp.net mvc中回发

时间:2015-09-21 14:39:16

标签: asp.net asp.net-mvc

请注意我是mvc和web开发的新手。 如何在没有回发的情况下从数据库中获取数据? 就像我点击更新网址时一样,我不想重新加载页面。

我的模特

 public partial class country
{
    public country()
    {
        this.cities = new HashSet<city>();
    }

    public int Id { get; set; }
    public string countryName { get; set; }
    public Nullable<bool> active { get; set; }

    public virtual ICollection<city> cities { get; set; }
}

我的观点

  @using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()

    <div id="tt">
        <div class="nicdark_bg_blue blueb" style="margin-top:10px;">
            <div class="row">
                <div class="col-md-6">
                    <div class="form-group">
                        <label for="addres">Country Name</label>
                        <br />
                        <input type="text" placeholder="countryName" class="form-control add" id="countryName" name="countryName" />
                    </div>

                </div>

                <div class="col-md-1">
                    <div class="form-group">
                        <label for="active">Passive</label>
                        <br />
                        <input type="checkbox"  class="form-control" value="true" id="active" name="active" style="width:34px" />
                        <input type="hidden" value="false"  name="active" />
                    </div>

                </div>



                <div class="form-group">
                    <div class="col-md-1">
                        <label for="s"></label>
                        <br />
                        <input type="submit" value="Create" class="btn btn-primary" id="s" style="margin-top:6px; width:80px; height:35px;" />
                    </div>
                    <div class="col-md-1">
                        <label for="hi"></label>
                        <input type="reset" value="Cancel" class="btn btn-danger" id="hi" style="margin-top:6px; margin-left:3px; width:80px; height:35px;" />
                    </div>
                </div>
            </div>
            </div>

    </div>

}

@if (ViewBag.e != null )
{ 
using (Html.BeginForm("Edit" ,"countries" , FormMethod.Post))
{
    @Html.AntiForgeryToken()

    <div id="ttt">
        <div class="nicdark_bg_blue blueb" style="margin-top:10px;">
            <div class="row">
                <div class="col-md-6">
                    <div class="form-group">
                        <label for="addres">Country Name</label>
                        <br />
                        <input type="text" placeholder="countryName" class="form-control add" id="countryName" name="countryName" value="@ViewBag.e.countryName" />
                    </div>

                </div>

                <div class="col-md-1">
                    <div class="form-group">
                        <label for="active">Passive</label>
                        <br />
                        @if(ViewBag.e.active==true)
                        { 
                        <input type="checkbox" class="form-control" value="true" id="active" name="active" style="width:34px"  checked="checked"/>
                        <input type="hidden" value="false" name="active" />
                        }
                        <br />
                        @if (ViewBag.e.active == false)
                        {
                            <input type="checkbox" class="form-control" value="true" id="active" name="active" style="width:34px"  />
                            <input type="hidden" value="false" name="active" />
                        }
                    </div>

                </div>



                <div class="form-group">
                    <div class="col-md-1">
                        <label for="s"></label>
                        <br />
                        <input type="submit" value="Edit" class="btn btn-primary" id="s" style="margin-top:6px; width:80px; height:35px;" />
                    </div>
                    <div class="col-md-1">
                        <label for="hi"></label>
                        <input type="reset" value="Cancel" class="btn btn-danger" id="hi" style="margin-top:6px; margin-left:3px; width:80px; height:35px;" />
                    </div>
                </div>
            </div>
        </div>

    </div>

}
}

@if (ViewBag.d != null)
{
    using (Html.BeginForm("Delete", "countries", FormMethod.Post))
    {
        @Html.AntiForgeryToken()

        <div id="tttt">
            <div class="nicdark_bg_blue blueb" style="margin-top:10px;">
                <div class="row">
                    <div class="col-md-6">
                        <div class="form-group">
                            <label for="addres">Country Name</label>
                            <br />
                            <input type="text" placeholder="countryName" class="form-control add" id="countryName" name="countryName" value="@ViewBag.d.countryName" />
                        </div>

                    </div>

                    <div class="col-md-1">
                        <div class="form-group">
                            <label for="active">Passive</label>
                            <br />
                            @if (ViewBag.d.active == true)
                            {
                                <input type="checkbox" class="form-control" value="true" id="active" name="active" style="width:34px" checked="checked" />
                                <input type="hidden" value="false" name="active" />
                            }
                            <br />
                            @if (ViewBag.d.active == false)
                            {
                                <input type="checkbox" class="form-control" value="true" id="active" name="active" style="width:34px" />
                                <input type="hidden" value="false" name="active" />
                            }
                        </div>

                    </div>



                    <div class="form-group">
                        <div class="col-md-1">
                            <label for="s"></label>
                            <br />
                            <input type="submit" value="Delete" class="btn btn-primary" id="s" style="margin-top:6px; width:80px; height:35px;" />
                        </div>
                        <div class="col-md-1">
                            <label for="hi"></label>
                            <input type="reset" value="Cancel" class="btn btn-danger" id="hi" style="margin-top:6px; margin-left:3px; width:80px; height:35px;" />
                        </div>
                    </div>
                </div>
            </div>

        </div>

    }
}

我的控制员:

 // GET: countries/Create
    public ActionResult Create()
    {
        return View();
    }

    // POST: countries/Create
    // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
    // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create([Bind(Include = "Id,countryName,active")] country country)
    {
        if (ModelState.IsValid)
        {
            country.countryName = country.countryName.ToUpper();
            db.countries.Add(country);
            db.SaveChanges();
            return RedirectToAction("Create");
        }

        return View(country);
    }

    // GET: countries/Edit/5
    public ActionResult Edit(int? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        country country = db.countries.Find(id);
        if (country == null)
        {
            return HttpNotFound();
        }
        Session["id"] = country.Id;
        ViewBag.e = country;
        return View("Create");
    }

    // POST: countries/Edit/5
    // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
    // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Edit([Bind(Include = "Id,countryName,active")] country country)
    {
        if (ModelState.IsValid)
        {
            int id = (int) Session["id"];
            country c = db.countries.Find(id);
            c.active = country.active;
            c.countryName = country.countryName;
            db.SaveChanges();
            return RedirectToAction("Create");
        }
        return View("Create");
    }

    public country eco (int id)
    {
        country c = db.countries.Find(id);
        return c ;
    }

    // GET: countries/Delete/5
    public ActionResult Delete(int? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        country country = db.countries.Find(id);
        if (country == null)
        {
            return HttpNotFound();
        }
        ViewBag.d = country;
        return View("Create");
    }

    // POST: countries/Delete/5
    [HttpPost, ActionName("Delete")]
    [ValidateAntiForgeryToken]
    public ActionResult DeleteConfirmed(int id)
    {
        country country = db.countries.Find(id);
        db.countries.Remove(country);
        db.SaveChanges();
        return RedirectToAction("Create");
    }

这是更新和删除的好方法吗?

1 个答案:

答案 0 :(得分:1)

你需要Ajax才能做到这一点。您应该有一个在控制器中进行提取的操作。

$.get( "@Url.Action('Action', 'Controller', { param1: 'something' })", function( data ) {
    //Put whatever you want to do with the data here
});

更多信息和示例Here