2017-05-09 1 views
0

Chaque fois que j'essaie de charger une vue partielle, elle n'a aucun style et ne lui applique pas de script java.Les vues partielles n'ont aucun style lorsque l'action a un paramètre? ASP.NET MVC 5

Voici un exemple: une vue partielle où l'utilisateur est capable de commenter un projet.

c'est le modèle que je utilise:

public class Comment 
{ 
    [Key] 
    public int Id { get; set; } 
    public string CommentDesc { get; set; } 

    public string ProjectManagerId { get; set; } 
    [ForeignKey("ProjectManagerId")] 
    public ApplicationUser ProjectManager { get; set; } 

    public int ProjectId { get; set; } 
    [ForeignKey("ProjectId")] 
    public Project Project { get; set; } 

} 

c'est l'action:

[HttpGet] 
    [MyAuthorize(Roles = "Project Manager")] 
    public ActionResult Comment() 
    { 
     return PartialView("Comment"); 

    } 

    [HttpPost] 
    [MyAuthorize(Roles = "Project Manager")] 
    public ActionResult Comment(int id,Comment Comment) 
    { 

      var userId = User.Identity.GetUserId(); 
      Comment Com = new Comment 
      { 
       ProjectManagerId = userId, 
       ProjectId= id, 
       CommentDesc = Comment.CommentDesc 
      }; 
      context.Comments.Add(Com); 
      context.SaveChanges(); 
      return RedirectToAction("Main", "Home"); 



    } 

c'est là où l'action est appelée:

<a title="Comment" class="CommentProj" href='@Url.Action("Comment", "Project",new { id = item.Project.Id })'> 
    <img src='~/Bikes/Comment.png' width="32px" height="32px" /> 
</a> 

et est la vue partielle:

@model Trial.Models.Comment 
@using (Html.BeginForm("Comment", "Project")) 
{ 

<div class="modal hide fade"> 
    <div class="modal-dialog"> 
     <div class="modal-content"> 
      <div class="modal-header"> 
       <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button> 
       <h4 class="modal-title">Comment</h4> 
      </div> 
      <div class="modal-body"> 
       <form> 
        @Html.TextAreaFor(m => m.CommentDesc, new { @class = "input-style form-control", @style = "background-color: transparent;", @placeholder = "Comment on the post", @rows = "6" })<br /> 
       </form> 
      </div> 
      <div class="modal-footer"> 
       <input type="submit" class="put-in btn btn-succes" value="Post" /> 
       <button type="button" class="btn btn-primary" data-dismiss="modal">Close</button> 
      </div> 
     </div> 
    </div> 
</div> } 

et voici comment il ressemble quand il est chargé: Here

+0

Une vue partielle est juste cela, partie d'une vue. Vous souhaitez effectuer un appel pour afficher votre vue partielle depuis l'intérieur d'un élément de vue complet. Vérifiez ceci pour des exemples; https://www.codeproject.com/Tips/617361/Partial-View-in-ASP-NET-MVC –

+0

En regardant votre code, la solution la plus simple sera de transformer votre vue partielle 'comment' en vue et en' ' Commentaire action' vous pouvez le retourner. De cette façon, tous les styles seront appliqués et javascript fonctionnera tant que vous incluez les balises de script nécessaires ou que vous les définissez dans un ensemble de scripts. J'espère que ça aide – Aim

Répondre

0

Exemple:

Votre action:

[HttpGet] 
    [MyAuthorize(Roles = "Project Manager")] 
    public ActionResult Comment(int id) 
    { 
     var model= new Comment{ 
      ProjectId=id 
     } 
     return PartialView("Comment",model); 

    } 

    [HttpPost] 
    [MyAuthorize(Roles = "Project Manager")] 
    public ActionResult Comment(Comment comment) 
    {  
      var com = new Comment 
      { 
       ProjectManagerId = User.Identity.GetUserId(), 
       ProjectId= id, 
       CommentDesc =comment.CommentDesc 
      }; 
      context.Comments.Add(com); 
      context.SaveChanges(); 
      return RedirectToAction("Main", "Home"); 
    } 

Votre PartialView:

@model Trial.Models.Comment 
@using (Html.BeginForm("Comment", "Project")) 
{ 

<div class="modal hide fade"> 
    <div class="modal-dialog"> 
     <div class="modal-content"> 
      <div class="modal-header"> 
       <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button> 
       <h4 class="modal-title">Comment</h4> 
      </div> 
     <form> 
     //Please use Html.BeginForm OR Ajax.BeginForm OR jQuery Ajax for submit form 
      <div class="modal-body"> 
       @Html.HiddenFor(model=>model.ProjectId) 
       @Html.HiddenFor(model=>model.ProjectManagerId) 
       @Html.TextAreaFor(m => m.CommentDesc, new { @class = "input-style form-control", @style = "background-color: transparent;", @placeholder = "Comment on the post", @rows = "6" })<br /> 
      </div> 
      <div class="modal-footer"> 
       <input type="submit" class="put-in btn btn-succes" value="Post" /> 
       <button type="button" class="btn btn-primary" data-dismiss="modal">Close</button> 
      </div> 
     </form> 
     </div> 
    </div> 
</div> 
} 

En vous question que je ne comprends pas comment vous soumettez form. donc s'il vous plaît utiliser Html.BeginForm OU Ajax.BeginForm OU jQuery Ajax pour soumettre forme.