0

I have a footer that is a view component. Inside that footer, I have newsletter and I want to do it as a partial view.

But my footer takes a model, and my newsletter also needs a model.

Now I cannot call the Newsletter.

How do I do it?

hasti.au
  • 81
  • 1
  • 2
  • 9

1 Answers1

0

You can use a ViewModel to contain both models, you can check this:

public class MyViewModel
{
    public FirstModel FirstModels { get; set; }
    public SecondModel SecondModels { get; set; }
}

public class FirstModel
{
    public int Id { get; set; }
    public string Name { get; set; }
}
public class SecondModel
{
    public int Id { get; set; }
    public string Name { get; set; }
}

And this is ViewComponent's view, just need to call your ViewModel, and you can use the two

Models in MyViewModel now:

@model MyViewModel

@Model.FirstModels.Name  //get the name from FirstModel
<br/>
<partial name="_newsletter" model="Model.SecondModels"> //get the name from SecondModel in Partial

Extra:

The script should be defined in your _Layout.cshtml, not partial view:

//<html> ........</html>

<script>     
FormSuccess = function () {
    alert('you are membership now');
};
</script>

Form In partial view:

<form asp-controller="Home" asp-action="NewsLetter" data-ajax="true" data-ajax-mode="post" data-ajax-success="FormSuccess">
<input asp-for="Name">  //Email
<input type="submit" />
</form>

Result:

enter image description here

Jerry Cai
  • 869
  • 1
  • 4
  • 4
  • Thanks so much . I Can solve with this solution – hasti.au May 27 '21 at 06:58
  • I want to do this way: If user send email on newsletter form after that he receive a message that :" you are membership now". I want to do it with ajax form. but I cant write my function .can you help me ? its really necessary @Tisa – hasti.au May 27 '21 at 07:00
  • @hasti.au alert("you are membership now") in ajax success. What do you mean' cant write my function', Could you please show your related code?My guess is, you should place javascript part in _Layout, not partial. – Jerry Cai May 27 '21 at 07:24
  • I want to do after user send his email in newsletter after that receive a message that is successful. I put in on my controller: /switch (newsLetter) //{ // case NewsLetterResult.EmailExist: // { // return Json(new // { // text = "Success" @Tisa – hasti.au May 27 '21 at 07:54
  • but I cant write my footer.js ,said to me I should set form (Form Tag ) in ajax with som attribute. I put: @*
    *@ but I cant write function FormSucess. can you help me write this function @Tisa
    – hasti.au May 27 '21 at 07:57
  • I did it before this attribute. but I think its problem is for my reference. because after submit I see this:{"text":"\u0639\u0636\u0648\u06CC\u062A \u0634\u0645\u0627 \u062F\u0631 \u062E\u0628\u0631 \u0646\u0627\u0645\u0647 \u0628\u0627 \u0645\u0648\u0641\u0642\u06CC\u062A \u0627\u0646\u062C\u0627\u0645 \u0634\u062F"} – hasti.au May 27 '21 at 08:54
  • My refrence: – hasti.au May 27 '21 at 08:55
  • @hasti.au You should better offer your related code and put them in your main post. Use F12 to check the script error – Jerry Cai May 27 '21 at 08:59
  • I see in F12.but there is not any this .so I think my reference is wrong. I should reference unobtrusive? – hasti.au May 27 '21 at 09:14
  • @hasti.au Yes, you should added this: https://cdnjs.com/libraries/jquery-ajax-unobtrusive – Jerry Cai May 27 '21 at 09:16
  • I Cant understand the mean of this solution: when I have first view model: site setting, now I need use the partial on it(email for newsletter). so I insert new property such public string NewsLetterEmail { get; set; } in the site setting viewmodel. right? then on the site seeting view model I can use : @Tisa – hasti.au May 30 '21 at 20:17