2016-03-29 46 views
0

Hala MVC'ye yeni geliyorum ama bunu gerçekten merak ediyorum. Create view işlevim var, ama neden indeksime geri oluşturduğum yeni verileri gösteremediğimi merak ediyordum? Burada MVC'de Görüntüleme/Oluşturma işlevi nasıl eklenir ve dizine güncellenir?

benim denetleyicisi geçerli:

 //GET /UserActivity/Create 
    public ActionResult Create() 
    { 
     UserActivityModels ua = new UserActivityModels(); 
     return View(userActivity); 
    } 


    // 
    // POST: /UserActivity/Create 
    [HttpPost] 
    public ActionResult Create(FormCollection formCollection) 
    { 
     try 
     { 
      UserActivityModels ua = new UserActivityModels(); 


      ua.Id = Int32.Parse(formCollection["Id"]); 
      ua.CreatedBy = Int32.Parse(formCollection["CreatedBy"]); 
      ua.CreatedOn = DateTime.Parse(formCollection["CreatedOn"]); 
      ua.ModifiedBy = Int32.Parse(formCollection["ModifiedBy"]); 
      ua.ModifiedOn = DateTime.Parse(formCollection["ModifiedOn"]); 
      ua.ContactId = formCollection["ContactId"]; 
      ua.StatusCode = Int32.Parse(formCollection["StatusCode"]); 
      ua.StateCode = Int32.Parse(formCollection["StateCode"]); 
      ua.EntityName = formCollection["EntityName"]; 
      ua.EntityId = Int32.Parse(formCollection["EntityId"]); 
      ua.ActivityType = Int32.Parse(formCollection["ActivityType"]); 
      ua.ActivityStatus = formCollection["ActivityStatus"]; 

      ua.DueDate = DateTime.Parse(formCollection["DueDate"]); 

      ua.ActualEndDate = DateTime.Parse(formCollection["ActualEndDate"]); 

      ua.MasqueradeOn = DateTime.Parse(formCollection["MasqueradeOn"]); 

      ua.MasqueradeBy = Int32.Parse(formCollection["MasqueradeBy"]); 


      return RedirectToAction("Index"); 
     } 

     catch 
     { 
      return View("Index"); 
     } 
    } 

modeli:

public class UserActivityModels 
{ 
    public int Id { get; set; } 

    public DateTime CreatedOn { get; set; } 

    public int CreatedBy { get; set; } 

    public int ModifiedBy { get; set; } 

    public DateTime ModifiedOn { get; set; } 

    public string ContactId { get; set; } 

    public string EntityName { get; set; } 

    public int EntityId { get; set; } 

    public int StatusCode { get; set; } 

    public int StateCode { get; set; } 

    public int ActivityType { get; set; } 

    public string ActivityStatus { get; set; } 

    public DateTime DueDate { get; set; } 

    public DateTime ActualEndDate { get; set; } 

    public DateTime MasqueradeOn { get; set; } 

    public int MasqueradeBy { get; set; } 

    public string ContactName { get; set; } 

    //public int TotalCount { get; set; } 

    // public List<string> userActivity { get; set; } 

} 
ben tıklayın "Gönder" butonuna zaman, yeni oluşturulan veri şimdi dizinde gösteriyor neden

kimse bana söyleyebilirdi mu ? senin Index yöntemle İçinde

  @{ 
    ViewBag.Title = "Index"; 
    Layout = "~/Views/Shared/_LayoutMain.cshtml"; 

    } 

    <h2>Index</h2> 

     @*<p> 
    @Html.ActionLink("Create New", "Create") 
</p>*@ 

    <h3>User Activity</h3> 
     @using (Html.BeginForm("index", null, FormMethod.Get)) 
     { 
<div class="row"> 

    <div class="col-sm-8"> 
     <div class="input-group"> 

      <input type="text" 
        name="filter" 
        value="@ViewBag.filter" 
        class="form-control" 
        style="display: inline" 
        placeholder="Search by Contact Name or Entity Name" /> 
      <span class="input-group-btn"> 
       <button class="btn btn-default" type="submit">Go</button> 
      </span> 

     </div> 
    </div> 
    <div class="pull-right col-lg-1"> 
     <a class="btn btn-success" data-modal="" href="/UserActivity/Create" id="btnCreate"> 
      <span class="glyphicon glyphicon-plus"></span> 
     </a> 
    </div> 
</div> 

<div class="table-responsive" style="margin-top:5px;"> 
    @{ 
     var grid = new WebGrid(
        Model, 
        canPage: true, 
        rowsPerPage: 5, 
        canSort: true); 
     //ajaxUpdateContainerId: "grid"); 

    //grid.Bind(Model, rowCount: (int)ViewData["totalCount"], autoSortAndPage: false); 

     grid.Pager(WebGridPagerModes.All); 

     @grid.GetHtml(
     htmlAttributes: new { id = "grid" }, 
    // id for ajaxUpdateContainerId parameter 
    fillEmptyRows: false, 
    tableStyle: "table table-striped", 
    mode: WebGridPagerModes.All, 
    columns: grid.Columns(
     //grid.Column("Id", "Id"), 
     grid.Column("Id", "Id", style: "col-lg-1", canSort: true), 
     grid.Column("CreatedBy", "CreatedBy", style: "col-lg-6"), 
     grid.Column("CreatedOn", header: "CreatedOn", style: "col-lg-2", canSort: true), 
     grid.Column("ModifiedBy", header: "ModifiedBy", style: "col-lg-2"), 
     grid.Column("ModifiedOn", header: "ModifiedOn", style: "col-lg-2"), 
     grid.Column("ContactId", header: "ContactId", style: "col-lg-2"), 
     grid.Column("EntityName", header: "EntityName", style: "col-lg-2"), 
     //grid.Column("EntityName", "EntityName", style: "col-lg-1", canSort: true), 
     grid.Column("EntityId", header: "EntityId", style: "col-lg-2"), 
     grid.Column("StatusCode", header: "StatusCode", style: "col-lg-2"), 
     grid.Column("StateCode", header: "StateCode", style: "col-lg-2"), 
     grid.Column("ActivityType", header: "ActivityType", style: "col-lg-2"), 
     grid.Column("ActivityStatus", header: "ActivityStatus", style: "col-lg-2"), 
     grid.Column("DueDate", header: "DueDate", style: "col-lg-2"), 
     grid.Column("ActualEndDate", header: "ActualEndDate", style: "col-lg-2"), 
     grid.Column("MasqueradeOn", header: "MasqueradeOn", style: "col-lg-2"), 
     grid.Column("MasqueradeBy", header: "MasqueradeBy", style: "col-lg-2"), 
     grid.Column("ContactName", header: "ContactName", style: "col-lg-2"), 
     //grid.Column("ContactName", "ContactName", style: "col-lg-1", canSort: true), 



     grid.Column(header: "Action", canSort: false, style: "action", 
format: @<text> 
     @Html.Raw("<a data-modal='' href='/UserActivity/Details/" + item.Id + "' id='" + item.Id + "' title='Detail'> <span class='glyphicon glyphicon-search'> </span> </a>") 
     @Html.Raw("<a data-modal='' href='/UserActivity/Edit/" + item.Id + "' id='" + item.Id + "' title='Edit'> <span class='glyphicon glyphicon-edit'> </span> </a>") 
     @Html.Raw("<a data-modal='' href='/UserActivity/Delete/" + item.Id + "' id='" + item.Id + "' title='Delete'> <span class='glyphicon glyphicon-trash'> </span> </a>") 
</text>) 

    )); 
    } 

</div> 
     } 

+0

What 'Index()'? Ve niçin '' catch' bloğunda '' View View (“Index”); Ve lütfen MVC sitesine gidin ve temelleri öğrenmek için derslerde çalışın. Siz POST yöntemi, "FormCollection formCollection" değil, "FormCollection" işlevini kullanmanız gerekmez), UserActivityModels modeli olmalıdır. Ve hiçbir zaman verileri saklayamazsın! –

+0

dizini burada ~/Görünümler/Paylaşılan/Dizin için anlamına gelir. Bu yüzden, create.cshtml dosyasında yeni veri oluşturmak, daha sonra veritabanına depolamak ve "submit" (gönder) düğmesine tıklandığında yeni verileri Index'e göstermek istiyorum. Yeni verilerin neden db'ye saklanmadığını merak ediyorum? ve ben zaten @StephenMuecke – Botski

+0

sorusuna create.cshtml sağladım. Başka bir yöntemi yeniden yönlendirdiğinizde nasıl görüntülenmesini beklersiniz (bu yöntemi bile göstermediniz!). MVC sitesine gidin ve temel bilgileri öğrenin –

cevap

0

, size Create yöntemde oluşturulan modeli almak zorunda ve bağımsız değişken olduğu gibi o kullanır: Burada

ve teşekkür

Görünüm/Endeksi ise görünümü döndürüyor.

Bir denetleyici yönteminden diğerine bir model geçirmenin bir yolu, TempData sözlüğünü kullanmaktır.

Peki yapabileceğiniz şudur: sizin Create yöntemde, sadece return RedirectToAction("Index"); çağırmadan önce, TempData modelinizi saklamak: Index yöntemde, Sonra

TempData["MyModel"] = ua; 

TempData gelen modeli almak ve geçmek görünüm:

var myModel = (UserActivityModels)TempData["MyModel"]; 

return View(myModel); 
+0

eşim denedim. Ama ortaya çıkıyor, datagrid ile ilgili bir sorun var. hata, "Bu işlem gerçekleştirilebilmesi için önce bir veri kaynağının bağlı olması gerekir." herhangi bir fikir nasıl? :( – Botski

+0

Bunun için farklı bir soru açmanız gerekebilir, ama bana nerede veri ızgarası kullandığınızı söyler misiniz? Dizin görünümünde? Html yardımcılarını kullandığınızı düşündüm ('EditorFor', LabelFor' vb. .)? –

+0

Evet, indekste datagrid kullanıyorum. İndeksimi soru üzerine güncelledim. – Botski