0

I am trying to learn ASP.NET MVC and ADO.NET. I am making a demo website in which there would be an admin panel. The admin would update the Daily Messages which will get reflected in the main homepage. I am using ASP.NET MVC. I have created the table in database as

create table DailyMsg
(
    Sno int primary key, 
    msg varchar(max)
);

This is my Daily Access Layer

public class DailyMsgs
{
    static string connectionString = @"data source = DELL\SQLEXPRESS01;initial catalog = amen; persist security info=True;Integrated Security = SSPI;";
    SqlConnection con = new SqlConnection(connectionString);

    DailyMsg dm = new DailyMsg();

    public string ViewDailyMessage()
    {
        SqlCommand com = new SqlCommand("sp_viewDsilyMSg", con);
        com.CommandType = CommandType.StoredProcedure;
        con.Open();
        string simpleValue = com.ExecuteScalar().ToString();
       
        con.Close();
        return simpleValue;
    }
}

My model class:

public partial class DailyMsg
{
    public int Sno { get; set; }
    public string msg { get; set; }
}

Now, I am stuck at this step. I don't know how to place this returned value simpleValue to the <h2> tag of my view.

My controller

DailyMsgs dailyMsgs = new DailyMsgs();

private amenEntities db = new amenEntities();

// GET: DailyMsgs
public ActionResult Index()
{
    return View();
}

My home page:

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

2
  • 1
    For what it's worth, I wouldn't try to mix in webforms, especially if you're learning. It's older and less used. It's still useful to know, but it's entirely separate from MVC. Trying to learn both at once will be harder. And if you never happen to learn webforms chances are you won't miss out. Commented Jan 8, 2022 at 16:29
  • @ScottHannen so, if I choose to work with views instead of webform, then how can I print this data to the view? Could you please guide me on that? Thanks! Commented Jan 8, 2022 at 16:48

1 Answer 1

3

Don't use webforms with MVC, use views instead.

  1. Create a view called Index.cshtml in the Views/Home directory. Or use the easy way - just right-click the Index() action in the controller and select Add View...

  2. At the top of the Index view add

    @model @<YourProjectName>.<Models>.DailyMsg
    

    and replace <YourProjectName>.<Models> with the actual namespace where DailyMsg class resides

  3. Currently, dailyMsgs.ViewDailyMessage() returns a string. You'll need to convert it to your model:

    // GET: DailyMsgs
    public ActionResult Index()
    {
        DailyMsg dailyMsg = new DailyMsg();
        // here I'm assuming your stored proc returns the daily message without the id
        // you should update ViewDailyMessage() to return a DailyMsg
        dailyMsg.msg = dailyMsgs.ViewDailyMessage();
        return View(dailyMsg);
    }
    
  4. Add the html:

    <div>
        @Model.msg
    </div>
    

The text should be displayed inside the div.

Ideally, you should update ViewDailyMessage() in the data layer to return your DailyMsg model rather than a string.


Additionally, please consider renaming your classes and methods as they are currently poorly named and confusing. Your data layer DailyMsgs and model DailyMsg have very similar names and perform completely different functions.

Give them appropriate names. I'd suggest renaming as follows:

// data layer
// DailyMsgs (rename) -> MessageDataProvider
public class MessageDataProvider
{
    // rename ViewDailyMessage -> GetDailyMessage
    public DailyMessageModel GetDailyMessage()
    {
    }
}

// model
// DailyMsg (rename) -> DailyMessageModel
public partial class DailyMessageModel
{
}

This way you can see the responsibility of each class just from the name (one is data layer, the other is a model).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.