2015-09-21 55 views
1

請問我是一位新的mvc和web開發人員。 如何從數據庫中獲取數據而無需回發? 就像當我點擊更新網址時,我不想重新加載頁面。從數據庫中獲取數據而無需在asp.net中回傳mvc

我的模型

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

這是更新和刪除的好辦法?

+0

歡迎SO - 你的文章是缺乏細節/過寬的時刻得到一個很好的答案,你能詳細到目前爲止,你已經嘗試任何代碼,而且細節您可能遇到的任何錯誤。礦石信息在這裏:http://stackoverflow.com/help/mcve –

+1

@PeterBailey請檢查我的代碼 – John

回答

1

您需要使用Ajax。你應該有一個在你的控制器中獲取的動作。

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

更多信息和示例Here

+0

請檢查我的代碼 – John